我想发布一个巨大的JSON对象,但是当我尝试时我得到了这个错误:
0 java.lang.OutOfMemoryError
1 at java.lang.String.<init>(String.java:432)
2 at java.lang.AbstractStringBuilder.toString(AbstractStringBuilder.java:642)
3 at java.lang.StringBuffer.toString(StringBuffer.java:723)
4 at java.io.StringWriter.toString(StringWriter.java:100)
5 at com.google.gson.Gson.toJson(Gson.java:528)
6 at com.google.gson.Gson.toJson(Gson.java:507)
7 at dk.companyoung.jobpatrulje.SendDialog$1.onClick(SendDialog.java:87)
这是我的代码:
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);
有人可能会告诉我什么是错的,也许可以给我一个例子:) 这会很棒!谢谢大家:)
答案 0 :(得分:3)
要在使用大型JSON对象时避免此错误,您需要使用流式JSON解析器 - http://wiki.fasterxml.com/JacksonInFiveMinutes#Streaming_API_Example。其中有两个适用于Android:GSON
和Jackson
。
我最喜欢的是Jackson
它非常简单,非常快。但是,您当然可以尝试使用GSON
:https://sites.google.com/site/gson/streaming
顺便在GSON
文档中,您可以找到问题的解释:
Most applications should use only the object model API.
JSON streaming is useful in just a few situations:
When it is impossible or undesirable to load the entire
object model into memory. This is most relevant on mobile
platforms where memory is limited.
在我付费的Android应用程序中,我在Jackson
上下文中使用了JSON
个大POST
个对象,但对于POST查询,我使用了Spring RestTemplate
图书馆。 :http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html。我不知道它对你有用。以下是我使用LinkedHashMap
库将巨大的Jackson
序列化为JSON并将其发布到远程服务器的代码:
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();