2014-10-30 73 views
0

我通過發佈參數來編寫從服務獲取json數據的代碼,但我沒有從webservice獲得完整響應,就像我想顯示500個定時問題一樣,但它只顯示20個問題,當先進的REST客戶端檢查服務,那麼Web服務給予充分的效應初探,但通過Android程序IAM沒有得到充分反應,我的代碼: -無法接收來自web服務的完整響應android

public static String postRequest(Context ct, String url, 
      List<NameValuePair> postvalues) throws IllegalStateException, 
      IOException 
{ 
    mContext = ct; 
    HttpPost post = null; 
    String content = null; 

    HttpParams params = new BasicHttpParams(); 
    int timeoutConnection = 1000 *1; 
    HttpConnectionParams.setConnectionTimeout(params, timeoutConnection); 


    int timeoutSocket = 60 * 1000 *30; 
    HttpConnectionParams.setSoTimeout(params, timeoutSocket); 

    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); 

    HttpClient client = new DefaultHttpClient(params); 
    String srvURL = mainurl + url; 

    // Log.i("Webservise", "url="+srvURL); 
    post = new HttpPost(srvURL); 
    post.setHeader("Content-type", 
      "application/x-www-form-urlencoded;charset=UTF-8"); 
    post.setHeader("Accept-Charset", "utf-8"); 
    post.setEntity(new UrlEncodedFormEntity(postvalues, HTTP.UTF_8)); 

    try { 

     HttpResponse response = client.execute(post); 
     HttpEntity entity = response.getEntity(); 

     is = entity.getContent(); 

     try { 
      BufferedReader reader = new BufferedReader(
        new InputStreamReader(is, "iso-8859-1"), 32); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line); 
      } 
      is.close(); 
      json = sb.toString(); 
      // Log.e("JSON", json); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 
     Log.e("Webservice call", "Response kp=" + json.toString()); 

    } catch (SocketTimeoutException ex) { 
     Toast.makeText(ct, "Request time out", Toast.LENGTH_SHORT).show(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (Exception ex) { 
     // Do something for all other types of exception. 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return json; 

} 

回答

0

這爲我工作。我使用EntityUtil將響應轉換爲字符串,而不是使用BufferedReader。 試試這個

try{ 
    HttpResponse response = client.execute(post); 

    json = EntityUtils.toString(response.getEntity()); 

    } catch (ClientProtocolException e) { 
     // writing exception to log 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // writing exception to log 
     e.printStackTrace(); 

    } 
相關問題