gpt4 book ai didi

android - 在 Android 中发布巨大的 JSON 对象

转载 作者:行者123 更新时间:2023-11-29 17:58:18 25 4
gpt4 key购买 nike

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

有人可以告诉我哪里出了问题,也许可以给我一个例子 :)这会很棒!谢谢大家:)

最佳答案

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

我最喜欢的是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 应用程序中,我在 POST 上下文中使用了带有巨大 JSON 对象的 Jackson 但对于POST 查询我使用了 Spring RestTemplate 库。 : http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html .我不知道它是否对你有用。这是我使用 Jackson 库将巨大的 LinkedHashMap 序列化为 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();

关于android - 在 Android 中发布巨大的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17650509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com