首先,我应该提一下,我已经看到其他人提出的有关此错误的其他问题,但我无法获得线索。
我正在为我的应用制作照片库,照片以字节形式插入网站数据库。我准备了一个Web服务,它将这些图片作为json字符串返回。一切都很好,直到网站数据库中的照片数量变高(返回的json字符串变得越来越大)
当我试图读取这个json并将其放入字符串时,我收到此错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:124)
at java.lang.StringBuilder.append(StringBuilder.java:271)
at org.kxml2.io.KXmlParser.readValue(KXmlParser.java:1345)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:390)
at org.kxml2.io.KXmlParser.next(KXmlParser.java:310)
at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:210)
at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:233)
at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:366)
at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:121)
at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:137)
at org.ksoap2.transport.Transport.parseResponse(Transport.java:63)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:100)
at ir.pargansystem.orkidehtalaei.OrkidehWebService.Call(OrkidehWebService.java:42)
at ir.pargansystem.orkidehtalaei.photogallery.PhotoGalleryViewPager$GetDataTask.doInBackground(PhotoGalleryViewPager.java:226)
at ir.pargansystem.orkidehtalaei.photogallery.PhotoGalleryViewPager$GetDataTask.doInBackground(PhotoGalleryViewPager.java:218)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
如何在不出现内存错误的情况下读取json并对其进行反序列化? 任何帮助,将不胜感激!
编辑1: 网络服务代码:
public String Call(String methodName) {
String loginUser = "user";
String loginPassword = "pass";
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, methodName);
PropertyInfo pi = new PropertyInfo();
pi.setName("username");
pi.setValue(loginUser);
pi.setType(String.class);
request.addProperty(pi);
pi = new PropertyInfo();
pi.setName("password");
pi.setValue(loginPassword);
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response = null;
try {
httpTransport.call(SOAP_ACTION + methodName, envelope);
response = envelope.getResponse();
} catch (Exception exception) {
response = exception.toString();
exception.printStackTrace();
}
return response.toString();
}
并在此处调用网络服务:
private class GetDataTask extends AsyncTask<Void, Void, Void> {
String jsonString;
@Override
protected Void doInBackground(Void... params) {
// Simulates a background job.
try {
OrkidehWebService fws = new OrkidehWebService();
jsonString = fws.Call("getGalleryData"); // I get the error here
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
try {
Gson gson = new Gson();
@SuppressWarnings("serial")
Type collectionType = new TypeToken<List<PhotoGalleryEntity>>() {
}.getType();
lstPhotos = gson.fromJson(jsonString, collectionType);
Collections.sort(lstPhotos, new Comparator<PhotoGalleryEntity>() {
public int compare(PhotoGalleryEntity o1, PhotoGalleryEntity o2) {
return o1.getPriority() - o2.getPriority();
}
});
}