2011-11-09 41 views
1

我试图发送一个JSON字符串到服务器,并从Web服务器中检索返回的结果JSON。我能够处理发布部分,但不知道如何检索相同请求中的结果。Android Web请求

private boolean sendFacebookDataToServer(String url) 
{ 
    // conect to server 
    // This method for HttpConnection 
    boolean isDataSend=false; 
    try { 
     HttpClient client = new DefaultHttpClient(); 

     HttpPost request = new HttpPost(url); 

     List<NameValuePair> value = new ArrayList<NameValuePair>(); 

     value.add(new BasicNameValuePair("facebook", createJsonFormatDataString())); 

     UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value); 

     request.setEntity(entity); 

     HttpResponse res = client.execute(request); 

     String[] status_String=res.getStatusLine().toString().trim().split(" "); 

     if(status_String[1].equals("200")){ 
      isDataSend= true; 
     } 


    } catch (Exception e) { 
     System.out.println("Exp=" + e); 
    } 
    return isDataSend; 
} 

或者是否有任何文章我可以参考理解它是如何完成的?

回答

3
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
    String bufstring = EntityUtils.toString(res.getEntity(), "UTF-8"); 
    isDataSend= true; 
} 

响应数据将在bufstring

编辑:

只是我

String[] status_String=res.getStatusLine().toString().trim().split(" "); 

    if(status_String[1].equals("200")){ 
     isDataSend= true; 
    } 
+0

client.execute后,我应该流为它(要求)更换您的代码(波纹管) ? –

+1

EntityUtils.toString会为你做 – Selvin

+0

cool ..所以我的服务器的JSON字符串存储在bufstring中? –