如何从URL读取BIG JSON内容 [英] How to read BIG JSON Conten from URL

查看:108
本文介绍了如何从URL读取BIG JSON内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im查找方法从URL读取大的json内容.我编写了一个简单的应用程序,用于测试许多功能.大多数功能会显示约40-45秒的时间.比返回内容(JSON文件很大) JSON文件的大小 90kb .. 2600行 第一次阅读所有内容但很慢(40-45秒)的功能

im lookin way to read big json content from url. Im write simple app where i test much funcitons . Most function show me time ~40-45 seconds. Than return content(JSON file realy big) Size of JSON FILE 90kb .. 2600 lines First funciton reading all content but very slow (40-45 seconds)

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并进行解析.

Function work perfect. But time :(. U guys can just input fucn to code , set url and parse.

第二个功能

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

这种功能很奇怪.我只获得部分内容,而仅获得一小部分大内容.

this funciton work but very strange. i get only PART of content and very small part of big content.

任何人也许都知道一些更好的东西,或者如何修复第二个功能以进行测试..这向我展示了获取内容需要多少时间. 或者,也许有人拥有其他功能而不是两个功能呢? 问候彼得.

Anyone maybe know some thing better or how to fix second function for test .. it's show me how much time need for getting content. Or maybe some one have other fucntion which faste than two this function ? Regards Peter.

推荐答案

该串联将花费太多时间.改用StringBuilder.

That concatenation will take too much time appending. Use a StringBuilder instead.

    StringBuilder response = new StringBuilder();

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

更新:

以下是来自网址的getJsonObject的示例:

Here is an example of a getJsonObject from a url:

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());

}

这篇关于如何从URL读取BIG JSON内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆