2014-05-12 79 views
1

我想从this URL得到JSON,但由于JSON很大,服务器加载JSON的速度很慢。 我得到JSON从网上这段代码:没有收到完整的数据

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     if (params != null) 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
       is, "UTF-8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
     Log.i("TAG", sb.toString()); 
     is.close(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.i("TAG", "Error converting result " + e.toString()); 
    } 

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

    // return JSON String 
    return jObj; 

} 

但我可以只拿到第一部分为JSON和我JSON仍未完成 请帮助我。

+0

你确定这是你的有效的JSON响应? – Setu

+0

是的,这是有效的 – Alireza

+0

Logcat可能无法完全打印大消息..你可以尝试使用system.out.print(message)。 –

回答

0

试试这种方法。

public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { 

     try { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 
      if (params != null) 
       httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      json = getResponseBody(httpEntity); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.i("TAG", "Error parsing data " + e.toString()); 
     } 

     return jObj; 

    } 

添加这个方法

public String getResponseBody(final HttpEntity entity) throws IOException, ParseException { 

     if (entity == null) { 
      throw new IllegalArgumentException("HTTP entity may not be null"); 
     } 

     InputStream instream = entity.getContent(); 

     if (instream == null) { 
      return ""; 
     } 

     if (entity.getContentLength() > Integer.MAX_VALUE) { 
      throw new IllegalArgumentException(

      "HTTP entity too large to be buffered in memory"); 
     } 

     StringBuilder buffer = new StringBuilder(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(instream, HTTP.UTF_8)); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 

     } finally { 
      instream.close(); 
      reader.close(); 
     } 
     return buffer.toString(); 

    } 
+0

嗨,你试过这个吗? –