Out of memory exception in gson.fromJson()

后端 未结 4 780
醉话见心
醉话见心 2021-01-04 21:21

I use the following code for converting Json string(strWebserviceResult) to my Object:

EntMyClass entMyClass = gson.fromJson(strWebserviceResult,EntMyClass.c         


        
4条回答
  •  北海茫月
    2021-01-04 21:49

    Try using the fromJson method that takes a JsonReader as input instead. This should allow you to not need the whole input string to be in memory at one time. Here's some sample code:

            final HttpURLConnection c = (HttpURLConnection) url.openConnection();
            final InputStreamReader isr = new InputStreamReader(c.getInputStream());
            final JsonReader reader = new JsonReader(isr);
            final EntMyClass entMyClass = GSON.fromJson(reader, EntMyClass.class);
            reader.close();
            c.disconnect();
    

提交回复
热议问题