POST huge JSON object in Android -


i post huge json object, when try got error:

0 java.lang.outofmemoryerror 1 @ java.lang.string.<init>(string.java:432) 2 @ java.lang.abstractstringbuilder.tostring(abstractstringbuilder.java:642) 3 @ java.lang.stringbuffer.tostring(stringbuffer.java:723) 4 @ java.io.stringwriter.tostring(stringwriter.java:100) 5 @ com.google.gson.gson.tojson(gson.java:528) 6 @ com.google.gson.gson.tojson(gson.java:507) 7 @ dk.companyoung.jobpatrulje.senddialog$1.onclick(senddialog.java:87) 

here code:

httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://blabla.dk");  //making post-objekt post p = new post(); p.companies = arr; p.name = activist.gettext().tostring(); p.phone = cpr.gettext().tostring(); p.password = "hey"; p.receipt = receipt.gettext().tostring();   gson gson = new gson();  //its fails here.                string jsonen = gson.tojson(p); httppost.setentity(new bytearrayentity(jsonen.tostring().getbytes("utf8")));  httpresponse status = httpclient.execute(httppost); 

can possibly tell me what's wrong , maybe give me example :) great! :)

to avoid error when work huge json objects need use streaming json parsers - http://wiki.fasterxml.com/jacksoninfiveminutes#streaming_api_example. there 2 of them suitable android: gson , jackson.

my favorite 1 jackson

it simple , fast. of course can try use gson: https://sites.google.com/site/gson/streaming

by way in gson docs can find explanation of problem:

 applications should use object model api.   json streaming useful in few situations:  when impossible or undesirable load entire   object model memory. relevant on mobile   platforms memory limited. 

in 1 of paid android application used jackson huge json objects in post context post queries used spring resttemplate library. : http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/resttemplate.html . don't know if useful you. here code serializing huge linkedhashmap json jackson library , posting remote server:

public linkedhashmap<string, object>  executeservercommand                                 (string commandtoexecute, linkedhashmap<string, object> parameters)        {               linkedhashmap<string, object> resultofoperation =                              new linkedhashmap<string, object>();     objectmapper mapparameterstofromjackson = new objectmapper();     stringwriter stringrepresentation = new stringwriter();     try {       mapparameterstofromjackson.writevalue(stringrepresentation, parameters);     }      catch (jsongenerationexception e)     {         e.printstacktrace();     }      catch (jsonmappingexception e)      {         e.printstacktrace();     }      catch (ioexception e)      {         e.printstacktrace();      }     httpheaders requestheaders = new httpheaders();     requestheaders.setcontenttype(mediatype.application_form_urlencoded);     string postdata = "params=" + stringrepresentation.tostring();     requestheaders.setcontentlength(postdata.length());     httpentity<string> requestentity =               new httpentity<string>(postdata,requestheaders);     httpcomponentsclienthttprequestfactory preconfiguredhttpinstance =                                new httpcomponentsclienthttprequestfactory();     resttemplate restfulrequest = new resttemplate(preconfiguredhttpinstance);     restfulrequest.setrequestfactory(preconfiguredhttpinstance);     restfulrequest.getmessageconverters().add(new stringhttpmessageconverter());     responseentity<string> responsefromserver = restfulrequest.postforentity(networkcommands.main_url + commandtoexecute,     requestentity, string.class);     string serverresponsebody = responsefromserver.getbody(); 

Comments