[Android] Http GET/POST Client Module


Android/안드로이드 프로그래밍 2012. 9. 3. 11:51



public class NetworkSapper {

protected static NetworkSapper _instance;


private Map <String, String> _map ;

private InnerHandler _handler;


private NetworkSapper(){

super();

_map = new HashMap<String,String>();

_handler = new InnerHandler(Looper.getMainLooper());    

}


public static NetworkSapper getInst(){

if(_instance==null) _instance = new NetworkSapper();

return _instance;

}

 

public void requestUrlGet( String aUrl, String aCmdType){

_map.clear();


new InnerGetThread(aUrl).start();

}


public void requestUrlPost( String aUrl, String aCmdTypeString aBody){

_map.clear();

new InnerPostThread(aUrl, aBody).start();

}


//==========JSON PARSING ===

public void parsingJson_Data(String response){

String kResponse = String.format("[%s]", response);

 

try{

JSONArray jArr = new JSONArray(kResponse);

for (int i=0; i <jArr.length(); i++){

JSONObject object = jArr.getJSONObject(i);

 // Parsing -> MAP 에 넣는 과정 * String 등 Response를 각자 필요한 방식으로 

    }

}

catch(Exception e){

}    

callbackHashMap(_map);

}

    

public void callbackHashMap(Map <String, String> aMap){

    //받은 MAP  처리

}

}

//================ Inner Class =======================

//================ Inner Class =======================

class InnerGetThread extends Thread{ //GET 방식

private String _url;

public InnerGetThread(String aUrl){

super();

_url = aUrl;

}


public void run(){

String kResultStr = null;

 

        try {

        HttpClient httpclient = new DefaultHttpClient();

        HttpGet httpget = new HttpGet(_url);

            HttpResponse response = httpclient.execute(httpget);

                HttpEntity resEntity = response.getEntity();

                if(resEntity != null){

                kResultStr = EntityUtils.toString(resEntity);

                Log.d("hwan","run ==========="+kResultStr);

                }

        }  catch (IOException  ex) {

        kResultStr = "Connection failed; " + ex.getMessage();

        }

        Message kMsg = Message.obtain();

        kMsg.obj = kResultStr;

        _handler.sendMessage(kMsg);

}

}


class InnerPostThread extends Thread{ //POST방식

private String _url;

private String _body;

public InnerPostThread(String aUrl, String aBody){

super();

_url = aUrl;

_body = aBody;

}


public void run(){

String kResultStr = null;

 

        try {

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(_url);

         

        MGSignalMan signalMan = MGSignalMan.getInst();

        String myApiKey = signalMan.getMyApiKey();

         

        if(myApiKey==null) return;

         

        httppost.setHeader("Content-type", "application/json");

        httppost.addHeader("x-api-key", myApiKey);

        String requestBody="foo bar";

         

        if(_body !=null){

        requestBody = _body;

        }

        httppost.setEntity(new StringEntity(requestBody)); 

         

            HttpResponse response = httpclient.execute(httppost);

             

                HttpEntity resEntity = response.getEntity();

                if(resEntity != null){

                kResultStr = EntityUtils.toString(resEntity);

                Log.i("RESPONSE",kResultStr);

                }

        }

           

        catch (IOException  ex) {

        kResultStr = "Connection failed; " + ex.getMessage();

        }


        Message kMsg = Message.obtain();

        kMsg.obj = kResultStr;

        _handler.sendMessage(kMsg);

}

}


//받은 Message 처리 

class InnerHandler extends Handler{


public InnerHandler(Looper aLooper){

super(aLooper);

}


@Override

public void handleMessage(Message $msg){

super.handleMessage($msg);

String kResult = (String) $msg.obj;

Log.d("hwan","kResult ========= "+kResult);

parsingJson_Data(kResult);

}

}


 }

삼성전자 갤럭시 S20 플러스 자급제폰, 코스믹 그레이, 256GB Apple 에어팟 프로 (노이즈캔슬링 방수 블루투스 5), MWP22KH/A, 단일 색상 삼성전자 갤럭시탭S6 10.5 128G WIFI, SM-T860N, 마운틴 그레이

WRITTEN BY
ShakeJ

트랙백  0 , 댓글  0개가 달렸습니다.
secret