본문 바로가기

옛글/안드로이드 프로그래밍

[Android] Http GET/POST Client Module

반응형

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);

}

}


 }

반응형