2010-05-18 6 views
2

을 사용하여 웹 서비스에서 데이터를 송수신하는 것은 내 Android 앱에서 웹 서비스로 요청을 보내고 그 대가로 안드로이드에서 파싱 한 웹 서비스의 XML 파일과 같은 데이터를받을 수 있습니다 ?android

감사

카이

+2

네, 어떤 종류의 서비스를 원하십니까? – JeremyFromEarth

+0

Android로 웹 서비스에 대한 출발점이 필요하다고 생각합니다. 이 기사는 좋은 시작입니다. IMHO http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/ –

답변

2

이 난 그냥 그 처리를 위해 쓴 방법이다. 제 경우에는 XML보다 훨씬 컴팩트하므로 JSON을 사용하여 데이터를받습니다. 나는 그런 식으로하고 JSON에서 개체를 변환하는 구글의 GSON 라이브러리를 사용하는 것이 좋습니다 : JsonReply 일부 데이터를 보유 단지 POJO이다

Gson gson = new Gson(); 
JsonReply result = gson.fromJson(jsonResult, JsonReply.class); 

. 귀하의 경우 gson 사용 방법에 대한 Google의 Java 문서를 볼 수 있습니다. 또한이 방법은 모든 종류의 문자와 함께 작동한다고 말해야합니다. 나는 주로 sendign 키릴 문자 데이터에 사용하고 있습니다.

public String postAndGetResult(String script, List<NameValuePair> postParameters){ 
    String returnResult = ""; 
    BufferedReader in = null; 
    try { 
     HttpParams httpParameters = new BasicHttpParams(); 
     HttpProtocolParams.setContentCharset(httpParameters, "UTF-8"); 
     HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8"); 
     HttpClient client = new DefaultHttpClient(httpParameters); 
     client.getParams().setParameter("http.protocol.version", 
       HttpVersion.HTTP_1_1); 
     client.getParams().setParameter("http.socket.timeout", 
       new Integer(2000)); 
     client.getParams().setParameter("http.protocol.content-charset", 
       "UTF-8"); 
     httpParameters.setBooleanParameter("http.protocol.expect-continue", 
       false); 
     HttpPost request = new HttpPost(SERVER + script + "?sid=" 
       + String.valueOf(Math.random())); 
     request.getParams().setParameter("http.socket.timeout", 
       new Integer(5000)); 
     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
       postParameters, "UTF-8"); 
     request.setEntity(formEntity); 
     HttpResponse response = client.execute(request); 
     in = new BufferedReader(new InputStreamReader(response.getEntity() 
       .getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String line = ""; 
     String NL = System.getProperty("line.separator"); 
     while ((line = in.readLine()) != null) { 
      sb.append(line + NL); 
     } 
     in.close(); 
     returnResult = sb.toString(); 
    } catch (Exception ex) { 
     return ""; 
    } finally { 
     if (in != null) { 
      try { 
       in.close(); 
      } catch (IOException e) { 
      } 
     } 
    } 
    return returnResult; 
} 

이 정보가 도움이되기를 바랍니다. 재미있게 보내십시오 :)

관련 문제