我正在调用RESTFUL API。我正在使用弹簧库。但是当执行restTemplate.exchange时,我得到了这个OutOfMemoryError。下面是我的代码片段和错误日志。 我正在使用此代码使用webservices从服务器获取数据。在某些设备上,这种方法很好但很少有设备抛出异常。
FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
formHttpMessageConverter.setCharset(Charset.forName("UTF8"));
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(formHttpMessageConverter);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
DeviceInfo deviceInfo = new DeviceInfo(mContext);
map.add("organizationId", "0");
map.add("groupCode", securityCode);
map.add("deviceId", deviceInfo.getDeviceID());
map.add("version_code", deviceInfo.getVersionCode() + "");
map.add("freshDataLoad", mode);
HttpHeaders headers = new HttpHeaders();
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
String respStr = responseEntity.getBody();
以下是错误日志
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
at java.lang.StringBuffer.append(StringBuffer.java:278)
at java.io.StringWriter.write(StringWriter.java:123)
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:189)
at org.springframework.util.FileCopyUtils.copyToString(FileCopyUtils.java:240)
at org.springframework.http.converter.StringHttpMessageConverter.readInternal(StringHttpMessageConverter.java:107)
at org.springframework.http.converter.StringHttpMessageConverter.readInternal(StringHttpMessageConverter.java:44)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:147)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:98)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:743)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:728)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:549)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:504)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:452)
at com.example.testapp.sync.SyncStrategy.getTestData(SyncStrategy.java:
at com.example.testapp.sync.SyncWorker$2.run(SyncWorker.java:126)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.os.HandlerThread.run(HandlerThread.java:61)
答案 0 :(得分:0)
当您的应用程序消耗大量内存时,可能会发生这种情况。 尝试优化您的应用程序代码。
此外,一个技巧可以帮助你:在处理此请求之前尝试显式调用垃圾收集器,这需要大量内存。
Call:Runtime.getRuntime()。gc();调用垃圾收集器。