2015-02-02 1 views
0

나는 안드로이드 애플 리케이션을 개발 중이며, 이제는 json, php, mysql을 사용하여 로컬 데이터베이스와 서버 database.im 사이에서 데이터 동기화를 수행해야합니다. 지금까지는 서버에 데이터를 보낼 수 있으며 데이터를 검색 할 수도 있습니다.하지만이 두 가지 작업을 동시에 수행하려고합니다.json을 사용하여 안드로이드와 동시에 데이터를주고받는 방법

 DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppostreq = new HttpPost(params[1]); 
     StringEntity se=null; 
     try { 
      se = new StringEntity(jsonobj.toString()); 
     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     se.setContentType("application/json;charset=UTF-8"); 
     se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8")); 
     httppostreq.setEntity(se); 
     try { 
      HttpResponse httpresponse = httpclient.execute(httppostreq); 
     } catch (ClientProtocolException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

그리고 내가 사용하는 데이터를 검색 : 는 내가이 명령을 보내

 ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("ville","L")); 
     // Envoie de la commande http 
     try{ 
      HttpClient httpclient1 = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(params[0]); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient1.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection " + e.toString()); 
     } 
     // Convertion de la requête en string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result=sb.toString(); 
     }catch(Exception e){ 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

내가 살전 보내고 동시에 일부 날짜를 검색하는 두 commandes을 결합하려는, 내가 아는 해달라고입니다 어떻게 할 것인가.

+0

일반적인 http 요청/응답 이외의 다른 기능은 무엇입니까? – tachyonflux

+0

난 그냥 보내고 싶었어. 예를 들어, 내가 특정 사용자에 대한 데이터를 검색하고자 할 때, 나는이 사용자 ID를 보내고 싶다. 그런 다음 서버는 적절한 정보를 반환한다. –

답변

0

나는 당신이 원하는 것을 얻는다 고 생각한다. 당신이 얻으려고하는 것은 당신이 어떤 데이터를 서버에 보내고 그 특정 요청에 대해 응답을 바로 받기를 원한다는 것이다. 그렇다면 당신은 namevalue 쌍으로 데이터를 전달할 것이고 응답은 httpresponse 객체에서 특정 요청에 대해 수신됩니다.

DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppostreq = new HttpPost(params[1]); 
    StringEntity se=null; 
    try { 
     se = new StringEntity(jsonobj.toString()); 
    } catch (UnsupportedEncodingException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
    se.setContentType("application/json;charset=UTF-8"); 
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8")); 
    httppostreq.setEntity(se); 
    try { 
     HttpResponse httpresponse = httpclient.execute(httppostreq); 
     HttpEntity entity = httpresponse.getEntity(); 
     is = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result=sb.toString(); 
    } catch (ClientProtocolException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error converting result " + e.toString()); 
    } 
+0

가능한 한 빨리이 솔루션을 사용해 보겠습니다. 문제가 해결 될 것입니다. –

관련 문제