2013-04-18 38 views
2

我嘗試從ulr獲取json數據。在android中未得到完整的http響應

我正在測試firefox海報插件中的url工作正常,獲得完整響應。

但在android沒有得到完整的http響應。

這是我的代碼

HttpClient client = new DefaultHttpClient(); 
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
                     // Limit 
HttpResponse response; 
JSONObject json = new JSONObject(); 

try { 
    HttpPost post = new HttpPost(API.ALL_PROPERTY_BUY); 

    /* 
    * json.put("fId", fname); json.put("fId", lname); 
    */ 
    StringEntity se = new StringEntity(json.toString()); 
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json")); 
    post.setEntity(se); 
    response = client.execute(post); 

    /* Checking response */ 
    if (response != null) { 
     InputStream in = response.getEntity().getContent(); // Get the 
                  // data in 
                  // the 
                  // entity 
     // System.out.println("Response is:"+ in.toString()); 
     InputStreamReader isr = new InputStreamReader(in); 
     BufferedReader br = new BufferedReader(isr); 

     String s = new String(); 
     temps = new String(); 
     while ((s = br.readLine()) != null) { 
      System.out.println("response is :" + s); 

     } 



    } 


} catch (Exception e) { 
    e.printStackTrace(); 

} 

我也是從谷歌搜索,嘗試與代碼但不解決我的問題。

請任何人幫助我。

+0

在清單文件中添加Internet權限。 – 2013-04-18 05:17:58

+0

@VivekKumarSrivastava所有權限添加和我的代碼工作正常,但問題是沒有得到完整的響應從webservice。 – Hemant 2013-04-18 05:19:36

+0

你可以使用String s = EntityUtils.toString(response.getEntity());並讓我知道如果這項工作。 – 2013-04-18 05:24:32

回答

2

試試這個...

StringBuffer stringBuffer = new StringBuffer(); 
      while ((s = br.readLine()) != null) { 
       stringBuffer.append(s); 
      } 

System.out.println("response is :" + stringBuffer); 

有時它不打印在控制檯上充分反應,但你得到你的變量全力應對...

解決方案:完全數據不logcat的或控制檯中顯示

+0

它的工作,但沒有得到充分的答覆。 – Hemant 2013-04-18 05:52:52

+1

你的迴應是非常大的限制變量?意味着你得到的答覆一半或沒有得到任何答覆請指定...?謝謝 – SBJ 2013-04-18 05:54:06

+0

獲得了一半的迴應。因爲我的回覆大小非常大。 – Hemant 2013-04-18 05:58:53