以下是我从服务器获取XML的代码,但是当XML很大时,我得到内存不足错误你能告诉我我的代码需要更改吗
@Override
protected String doInBackground(String... arg0) {
String result = "";
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(getString(R.string.requesturl));
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity());
}
} catch (Exception e) {
}
return result;
}
@Override
protected void onPostExecute(String result )
{
progressDialog.dismiss();
ParseResponse parse=new ParseResponse();
InputSource is = new InputSource(new StringReader(result));
Gesmes gesmes=parse.parseXML(is);
}
当XML很小时似乎工作正常 这是我的Logcat:
03-31 15:01:25.547: E/AndroidRuntime(9791): java.lang.OutOfMemoryError
03-31 15:01:25.547: E/AndroidRuntime(9791): at java.util.ArrayList.add(ArrayList.java:118)
03-31 15:01:25.547: E/AndroidRuntime(9791): at com.parser.ParseResponse.parseXML(ParseResponse.java:64)
03-31 15:01:25.547: E/AndroidRuntime(9791): at com.europeanexchangerates.MainActivity$AsyncCurrency.onPostExecute(MainActivity.java:426)
03-31 15:01:25.547: E/AndroidRuntime(9791): at com.europeanexchangerates.MainActivity$AsyncCurrency.onPostExecute(MainActivity.java:1)
03-31 15:01:25.547: E/AndroidRuntime(9791): at android.os.AsyncTask.finish(AsyncTask.java:632)
03-31 15:01:25.547: E/AndroidRuntime(9791): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-31 15:01:25.547: E/AndroidRuntime(9791): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-31 15:01:25.547: E/AndroidRuntime(9791): at android.os.Handler.dispatchMessage(Handler.java:102)
03-31 15:01:25.547: E/AndroidRuntime(9791): at android.os.Looper.loop(Looper.java:136)
03-31 15:01:25.547: E/AndroidRuntime(9791): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-31 15:01:25.547: E/AndroidRuntime(9791): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 15:01:25.547: E/AndroidRuntime(9791): at java.lang.reflect.Method.invoke(Method.java:515)
03-31 15:01:25.547: E/AndroidRuntime(9791): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-31 15:01:25.547: E/AndroidRuntime(9791): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
答案 0 :(得分:1)
答案 1 :(得分:0)
对于这种类型的错误,您将此行添加到主要代码中,可能会为您的应用程序带来更多内存...
<application
android:largeHeap="true"
>
..........
................
</appliaction>
答案 2 :(得分:0)
您应该考虑使用SAX解析器来解析XML,而不是使用largeHeap =“true”。 DOM解析器最终会导致大型xmls的内存不足错误,并使大型文件的解析速度非常慢。
答案 3 :(得分:0)
尝试使用android:largeHeap =“true”来增加你的app中的应用堆大小。某些设备具有应用程序使用的固定内存量,例如每个应用程序64 MB。或者使用缩放和缓存来修复我的应用程序使用的内存。看看这些链接http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
如何增加Android应用程序的堆大小?
谢谢。