1

我想发布一个巨大的 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);

有人可以告诉我出了什么问题,也许可以给我一个例子:) 太好了!感谢大家 :)

4

1 回答 1

3

为避免在处理大型 JSON 对象时出现此错误,您需要使用流式 JSON 解析器 - http://wiki.fasterxml.com/JacksonInFiveMinutes#Streaming_API_Example。其中有两个适用于 Android:GSONJackson.

我最喜欢的是杰克逊

它真的很简单而且非常快。但当然你可以尝试使用GSONhttps ://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 。不知道对你有没有用。这是我使用库将大量序列化为LinkedHashMapJSONJackson并将其发布到远程服务器的代码:

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();
于 2013-07-15T09:22:15.807 回答