一尘不染

使用ANDROID中的json解析大小约为3MB的数据?

json

我必须通过JSon通过大小约为3MB的HTTP请求来解析数据,但是我正在使用的解析器无法做到这一点。这是JSon解析器:

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

任何帮助将不胜感激。谢谢


阅读 273

收藏
2020-07-27

共1个答案

一尘不染

您正在解析内存中的整个3MB字符串。它导致内存不足异常。解析流中的大数据:

2020-07-27