android - 如何从 URL 读取 BIG JSON 内容

标签 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);
}

这个功能可以工作,但很奇怪。我只获得部分内容和大内容的一小部分。

任何人可能都知道更好的事情或如何修复第二个功能以进行测试..它告诉我获取内容需要多少时间。 或者也许有人有其他功能比两个这个功能更快? 问候彼得。

最佳答案

该连接将花费太多时间进行附加。请改用 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());

}

关于android - 如何从 URL 读取 BIG JSON 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25803443/

相关文章:

javascript - 整数被错误地解析为字符串

node.js - 如何在nodeJS中将.doc文件读取/重写为xml?

android - 在 XML 文件中使用图表引擎定位图形

java - 让 Android 应用监听共享链接

android - 在 Android 应用程序中使用 Admob - 多个 dex 文件定义

Java JSONObject 获取 child

java - 将 Jackjson JSON 对象从 JSP 传递到 JavaScript 函数

android - 在 Android 中仅更新一次获取当前(不是最后一个)位置

javascript - JSON 中的 HTML 不存在 "should"

java - 如何使用 DOM 解析 java 中的内联/混合内容 xml 元素