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 aCmdType, String 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);
}
}
}
'옛글 > 안드로이드 프로그래밍' 카테고리의 다른 글
[Android] 버튼 클릭 효과 주기 (0) | 2012.09.24 |
---|---|
[Android] 안드로이드 HTML 6자리 컬러표 (0) | 2012.09.24 |
[Android] Class Instance Shared (0) | 2012.09.03 |
[Android] Json Format Write / Read (0) | 2012.09.03 |
[Android] Activity has leaked window 오류를 잡아내자 (0) | 2012.03.14 |