解析大小有关使用JSON在ANDROID 3MB的数据? [英] Parse data of size about 3MB using json in ANDROID?

查看:161
本文介绍了解析大小有关使用JSON在ANDROID 3MB的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要解析由大约3MB,通过JSON大小的HTTP请求数据,但我使用的解析器是无法做到的。这里是JSON解析器:

I have to parse data by HTTP request of size about 3MB, through JSon, but the parser that I am using is unable to do so. here is the JSon parser:

public static JSONObject getJSONfromURL(String url){

    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

    }catch(Exception e){
          //  Log.e("log_tag", "Error in http connection "+e.toString());
    }

    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),102400);

        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();

    }catch(Exception e){
           // Log.e("log_tag", "Error converting result "+e.toString());
    }

    try{

        jArray = new JSONObject(result);            
    }catch(JSONException e){
           // Log.e("log_tag", "Error parsing data "+e.toString());
    }
    return jArray;
}

任何帮助将真正成为AP preciated。谢谢

Any help will really be appreciated. THANKS

推荐答案

您正在分析在记忆整个3MB字符串。它会导致内存溢出异常。在流解析大数据:

You're parsing whole 3MB string in memory. It causes out of memory exception. Parse large data in stream:

  • JsonReader since API level 11
  • Android JSON library or for large data Jackson Streaming API

这篇关于解析大小有关使用JSON在ANDROID 3MB的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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