当前位置: 首页 > 面试题库 >

OutOfMemoryError:收到2.3 MB的XML响应时

尉迟阳煦
2023-03-14
问题内容

以下是连接到Web服务并从服务器接收XML响应的简单方法,该响应约为2.3MB,我正在 OutOfMemoryError
(我已经提到了)但无法找到自己的路,卡住得很严重

public synchronized String getUpdates(boolean news) throws Exception {
        String response = null; 
        HttpPost httppost;
        DefaultHttpClient httpclient;
        ResponseHandler <String> res=new BasicResponseHandler();  
        List<NameValuePair> nameValuePairs;
        httppost = new HttpPost(context.getString(R.string.SYNCURL));  
        HttpParams params = new BasicHttpParams();  
        HttpProtocolParams.setContentCharset(params, "UTF-8");

        String lastupdate = null;
        SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        lastupdate = preferences.getString(LAST_UPDATE, DatabaseHelper.updateDate);
        if(news) lastupdate = preferences.getString(LAST_UPDATE_NEWS, lastupdate);

        httpclient = new DefaultHttpClient(params);
        nameValuePairs = new ArrayList<NameValuePair>(2);  
        nameValuePairs.add(new BasicNameValuePair("reqDTTM", lastupdate));
        if(news)nameValuePairs.add(new BasicNameValuePair("doAction", "news"));
        if(!preferences.getString(LAST_UPDATE, "").equalsIgnoreCase(DatabaseHelper.updateDate)) {
            String timezon = UDFHelper.getTimeZon(context);
            nameValuePairs.add(new BasicNameValuePair("tz", timezon));
        }
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
        response = httpclient.execute(httppost, res);
        return response;
}

在这里,它导致尝试检索大文本的问题String response

响应= httpclient.execute(httppost,res);

我将响应存储在String中。那是问题吗?那是什么替代解决方案?

这是LogCat

09-23 10:07:31.654: INFO/ActivityManager(59): Displayed activity uk.co.dodec.rcgpapp/.SplashScreen: 1267 ms (total 1267 ms)
09-23 10:07:35.854: INFO/dalvikvm-heap(334): Grow heap (frag case) to 3.213MB for 262160-byte allocation
09-23 10:07:38.203: INFO/dalvikvm-heap(334): Grow heap (frag case) to 3.586MB for 524304-byte allocation
09-23 10:07:42.753: INFO/dalvikvm-heap(334): Grow heap (frag case) to 4.336MB for 1048592-byte allocation
09-23 10:07:52.024: INFO/dalvikvm-heap(334): Grow heap (frag case) to 5.836MB for 2097168-byte allocation
09-23 10:08:09.844: INFO/dalvikvm-heap(334): Grow heap (frag case) to 8.836MB for 4194320-byte allocation
09-23 10:08:09.864: INFO/ActivityManager(59): Process com.android.mms (pid 227) has died.
09-23 10:08:45.984: INFO/dalvikvm-heap(334): Forcing collection of SoftReferences for 8388624-byte allocation
09-23 10:08:46.033: ERROR/dalvikvm-heap(334): Out of memory on a 8388624-byte allocation.
09-23 10:08:46.033: INFO/dalvikvm(334): "AsyncTask #1" prio=5 tid=7 RUNNABLE
09-23 10:08:46.033: INFO/dalvikvm(334):   | group="main" sCount=0 dsCount=0 s=N obj=0x43e54fd8 self=0x224d58
09-23 10:08:46.033: INFO/dalvikvm(334):   | sysTid=340 nice=10 sched=0/0 cgrp=bg_non_interactive handle=2248344
09-23 10:08:46.033: INFO/dalvikvm(334):   | schedstat=( 1248172861 2043417240 1093 )
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.util.CharArrayBuffer.expand(CharArrayBuffer.java:~59)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.util.CharArrayBuffer.append(CharArrayBuffer.java:77)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.util.EntityUtils.toString(EntityUtils.java:136)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:76)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-23 10:08:46.033: INFO/dalvikvm(334):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
09-23 10:08:46.033: INFO/dalvikvm(334):   at uk.co.dodec.rcgpapp.helper.HttpHelper.getUpdates(HttpHelper.java:172)
09-23 10:08:46.033: INFO/dalvikvm(334):   at uk.co.dodec.rcgpapp.SplashScreen.getUpdate(SplashScreen.java:155)
09-23 10:08:46.033: INFO/dalvikvm(334):   at uk.co.dodec.rcgpapp.SplashScreen$getUpdates.doInBackground(SplashScreen.java:126)
09-23 10:08:46.033: INFO/dalvikvm(334):   at uk.co.dodec.rcgpapp.SplashScreen$getUpdates.doInBackground(SplashScreen.java:1)
09-23 10:08:46.043: INFO/dalvikvm(334):   at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-23 10:08:46.043: INFO/dalvikvm(334):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-23 10:08:46.043: INFO/dalvikvm(334):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-23 10:08:46.043: INFO/dalvikvm(334):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-23 10:08:46.043: INFO/dalvikvm(334):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-23 10:08:46.043: INFO/dalvikvm(334):   at java.lang.Thread.run(Thread.java:1096)
09-23 10:10:37.353: WARN/dalvikvm(334): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
09-23 10:10:37.375: ERROR/AndroidRuntime(334): FATAL EXCEPTION: AsyncTask #1
09-23 10:10:37.375: ERROR/AndroidRuntime(334): java.lang.RuntimeException: An error occured while executing doInBackground()
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.lang.Thread.run(Thread.java:1096)
09-23 10:10:37.375: ERROR/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.util.CharArrayBuffer.expand(CharArrayBuffer.java:59)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.util.CharArrayBuffer.append(CharArrayBuffer.java:77)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.util.EntityUtils.toString(EntityUtils.java:136)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.util.EntityUtils.toString(EntityUtils.java:146)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:76)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at uk.co.dodec.rcgpapp.helper.HttpHelper.getUpdates(HttpHelper.java:172)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at uk.co.dodec.rcgpapp.SplashScreen.getUpdate(SplashScreen.java:155)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at uk.co.dodec.rcgpapp.SplashScreen$getUpdates.doInBackground(SplashScreen.java:126)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at uk.co.dodec.rcgpapp.SplashScreen$getUpdates.doInBackground(SplashScreen.java:1)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-23 10:10:37.375: ERROR/AndroidRuntime(334):     ... 4 more
09-23 10:10:37.393: WARN/ActivityManager(59):   Force finishing activity uk.co.dodec.rcgpapp/.SplashScreen

我尝试了以下不起作用

HttpResponse r = httpclient.execute(httppost);

HttpEntity entity = r.getEntity();

InputStream in = new ByteArrayInputStream(EntityUtils.toByteArray(entity));

File fl = new File(Environment.getDataDirectory().toString() + "/data/" + context.getPackageName() + "/" + FOLDER_NAME);
if(!fl.exists()) fl.mkdir();

String PATH = Environment.getDataDirectory().toString() + "/data/" + context.getPackageName() + "/" + FOLDER_NAME + "/" + "update.xml";
FileOutputStream f = new FileOutputStream(PATH);

byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = in.read(buffer)) > 0) {
    f.write(buffer, 0, len1);
}
f.close();

问题答案:

That is indeed a problem: don’t do that. You should write the response to a
file or parse the stream but definitely don’t try generating a 2.3 MB string.
Write a response handler that returns the
HttpEntity
behind the response, then get the InputStream using the
getContent
method, then save that to a file and then process the file.



 类似资料:
  • 问题内容: 首先,这个问题看起来像是 SOAP响应非常大-Android-内存不足错误 主题。 由于我的英语能力很弱,并且这个问题很相似,为了便于理解,我复制了一些陈述段落。 我有一个需要通过SOAP调用将大量数据下载到Web服务的应用程序。然后将响应发送到显示XML文件的函数。 数据大小超过11MB,并且每次都有 java.lang.OutOfMemoryError 。 修改Web服务以提供较少

  • 我定义了一个类来存储我的应用程序的配置数据。我想将其实例保存到xml并为此使用XStream。但是当我尝试编写实例时,我总是遇到内存错误。 这是我的类定义: ...等所有标准getter和setter 下面是我将单个对象导出为xml的处理程序 每次我抛出“线程中的异常”JavaFX应用程序线程“java.lang.OutOfMemoryError:java堆空间”。我不明白为什么这么简单的类会抛出

  • 问题内容: 我对iOS应用程序开发非常陌生,并且从服务器收到以下响应: 请任何人帮助我了解如何在我的应用程序中使用员工ID和员工姓名。 问题答案: 您的JSON数据看起来像“嵌套JSON”,这意味着您必须将其反序列化两次。 第一个反序列化从您的JSON数据中提取一个字符串: 现在是字符串 再次是JSON数据。第二个反序列化提取数组: 现在您可以像访问它

  • 下面是关于如何设置messenger机器人的Facebook教程-使用ngrok设置我的webhook。本地测试一切顺利,但在向bot发送消息时仍然没有收到任何响应。 韩国https://ngrok.com/ facebook教程https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start/

  • 我正在使用Volley向API发出GET请求: 预期的JSON对象响应很大(可能高达500 KB)。我无法在日志中看到完整的响应。仅显示前50行左右。我还得到了info: BasicNetwork.log慢速请求:请求的HTTP响应= 这意味着请求需要超过3000毫秒。 尝试过的事情: 我已经在手机的开发者选项中将记录器缓冲区大小增加到1M。 原因可能是什么?当它很大的时候,响应是大块发送的吗?如

  • 我使用PHP在我的服务器中通过以下功能向FCM发送通知: 响应是这样的。在文件中,https://firebase.google.com/docs/cloud-messaging/http-server-ref 多播id、成功、失败参数在响应中是必需的,但是。 这是什么原因?