如何从URL读取BIG JSON内容

时间:2014-09-12 07:56:44

标签: android json parsing url

我正在寻找从url中读取大json内容的方法。我写了简单的应用程序,我测试了很多功能。大多数功能显示我的时间~40-45秒。比返回内容(JSON文件真的很大) JSON文件的大小 90kb .. 2600行 第一个功能读取所有内容但非常慢(40-45秒)

public  String readJsonFromUrl(String urls)  {
         String content = "";
         URL myLink = null;
         String inputLine=null;
        try {
            myLink = new URL(urls);
         } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
        }
         BufferedReader in = null;
        try {
            in = new BufferedReader(
                         new InputStreamReader(
                         myLink.openStream()));
        } catch (IOException e) {
        }
        try {
            while ((inputLine = in.readLine()) != null)
                 content =content+inputLine;
        } catch (IOException e1) {
            // TODO Auto-generated catch block

        }
         try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       return content;
      }

功能完善。但是时间:(。大家可以输入fucn代码,设置url和解析。

第二功能

public void readJSONFromUrl(String urls) throws IOException, URISyntaxException
    {
        InputStream is = null;
        String response = "";
        String url=urls;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
        HttpGet httpGet = new HttpGet(url);
        httpResponse = httpClient.execute(httpGet);
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
        Log.i("RESPONSE","RESPONSE = "+response);
}
这个功能工作但非常奇怪。我只得到内容的一部分和大部分内容。

任何人都可能更了解某些事情或如何修复第二个功能进行测试..它会告诉我需要多长时间来获取内容。 或者也许有些人有其他功能比两个功能更紧固? 关心彼得。

2 个答案:

答案 0 :(得分:3)

这种连接将花费太多时间追加。请改用StringBuilder。

    StringBuilder response = new StringBuilder();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    content = response.toString();

<强>更新

以下是来自url的getJsonObject的示例:

private static JSONObject getJSONObject(String _url) throws Exception {
    if (_url.equals(""))
        throw new Exception("URL can't be empty");

    URL url = new URL(_url);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setDoInput(true);
    conn.setRequestProperty("User-Agent", "android");
    conn.setRequestProperty("Accept", "application/json");
    conn.addRequestProperty("Content-Type", "application/json");
    BufferedReader in = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));

    if (!url.getHost().equals(conn.getURL().getHost())) {
        conn.disconnect();
        return new JSONObject();
    }
    String inputLine;
    StringBuilder response = new StringBuilder();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    conn.disconnect();

    return new JSONObject(response.toString());

}

答案 1 :(得分:0)

第二个功能不可能仅为您提供一小部分内容。问题不在于响应或流读取,而在于你试图在LogCat中显示那么大的内容:LogCat有一个限制(见Android webservice GET request replies only a part of the XML responseWhat is the size limit for Logcat and how to change its capacity?; 90kb超过了LogCat的缓冲区)。这意味着您的变量响应 包含完整的JSON,但 LogCat未显示完整变量。只需解析你的JSON,一切都应该有效。