이 블로그 검색

2011년 3월 20일 일요일

일반 Server 통신과 AIDL을 이용한 방식

* 사실 일반 Server-Client 통신에서는 아래와 같은 문장만 있으면 Server에서 내려준 정보를 받기에 충분하다.

HttpClient mHttpClient = null;
String response;
String strUrl = "http://220.103.225.114:8083/search/getAddr.do?userid=1300072071621&sk=1bfd94bd74ef0b9f8305af5b21e79892&addr_mode=S&cnt_yn=Y";

 class TransThread extends Thread{   //Key
  public void run(){
   Log.e("", "run");

   response = null;

   try{
    Log.e("", "try");

   /* Server 통신 하는 부분 Start */
   URI uri = new URI(strUrl);  

    HttpGet request = new HttpGet();    // request 서버 접속 요청  
    request.setURI(uri);  

    HttpResponse httpResponse = mHttpClient.execute(request);  
   // HttpClient 형 객체 mHttpClient - Server에서 전송하는 Data가 담긴다. 
   response = EntityUtils.toString(httpResponse.getEntity()).trim(); 
  // String 형 객체 response에 server에서 날려준 정보를 Entity별로 담는다.
    Log.e("", "response" + response);

    messageProc(response); //  --> Jason Parser    }catch(Exception e){
    Log.e("", "catch");
    e.printStackTrace();
   }
  }
 }

위의 Server 통신 부분을 Thread에 넣고 안드로이드 Service를 호출해서 받는다.

public void startTransaction(HttpClient mHttpClient, Context ctx, String url, String params){
  if(readyTransaction(ctx)){
   //readyTransaction에서 true가 떨어졌을 경우
   strUrl = url;
   strParams = params;

   this.mHttpClient = mHttpClient;
   if(this.mHttpClient != null){

   }
   worker = new TransThread();   // Thread 생성
   worker.start();    // Thread 시작
  }
 }

 public void doGetUpgrade() throws RemoteException {
   // TODO Auto-generated method stub
   TestSource ts = new TestSource(getApplicationContext());
   ts.startTransaction(mHttpClient, getApplicationContext());   //startTransaction 호출
}


/*  ServiceConnection 객체 생성 Start*/
 ServiceConnection srvConn = new ServiceConnection() {  

  @Override
  public void onServiceDisconnected(ComponentName name) {
   // TODO Auto-generated method stub
   updateService = null;
  }

  @Override
  public void onServiceConnected(ComponentName name, IBinder service) {   // 요 녀석이네...
   // TODO Auto-generated method stub
   updateService = IService.Stub.asInterface(service);     // 이부분이 AIDL 을 처리하는 부분
   Log.e("", "service");
   if(updateService!=null){
    try{
     updateService.doGetUpgrade();   //doGetUpgrade 호출 
     }catch(Exception e){
     e.printStackTrace();
    }
   }
  }
 };

/* ServiceConnection 객체 생성 End */

/* onResume  Start */
 protected void onResume() {
 super.onResume();

  Intent intent = new Intent("kr.softcast.Metroi.Service.StationService.ACTION");
  this.bindService(intent, srvConn, Context.BIND_AUTO_CREATE); 
   // bindService에서 srvConn 호출  --> 원격 호출 Service (IPC)
}


/*** Jason Parsing 부분 ***/
void messageProc(String response) {
  // TODO Auto-generated method stub
  Log.e("", "messageProc");
 
  this.response = response;
  getResult(response.trim()); 
 }
 boolean bSuccess;
 ArrayList<Station> stationList = new ArrayList<Station>();
  //JSON object parse
 private void getResult(String response){  
   //파씽 - 이미 string 형태로 받아온 response를 Jason으로 다시 파씽
  Log.e("getresult", response);
  JSONObject jsonObj = null;
  JSONArray jsonArr = null;
  try{
   jsonObj = ServerUtils.parserMSG(response);
  }catch(Exception e){
   e.printStackTrace();
  }
 
  try{
   bSuccess = jsonObj.getBoolean("success");
  }catch(Exception e){
   e.printStackTrace();
  }
 
  try{
   if(!bSuccess){ //false
   
   }else{
   
   }
  }catch(Exception e){
  
  }
 }

//* JSONObject *//

import org.json.JSONException;
import org.json.JSONObject;
public class ServerUtils {
 // json object
 public static JSONObject parserMSG(String msg) {
  JSONObject jObj = null;
  try {
   jObj = new JSONObject(msg.trim());  // 생성자 인자에 때려 넣으면 자동으로 파씽되나 보네...
  } catch (JSONException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return null;
  }
  return jObj;
 }
}

댓글 없음:

댓글 쓰기