开发者

Parse data of size about 3MB using json in ANDROID?

开发者 https://www.devze.com 2023-03-09 17:36 出处:网络
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:

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

Any help will really be appreciated. THANKS


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
0

精彩评论

暂无评论...
验证码 换一张
取 消