當我刪除requestQuery.add()行 - 除網絡數據不存在外,所有工作都正常。加入requestQuery.add()之後 - 我得到這樣的:爲什麼在調用Volley.RequestQuery.add()時出現OutOfMemory異常?
回溯:
08-29 18:15:53.071 6305-7075/? E/dalvikvm-heap﹕ Out of memory on a 840969348-byte allocation.
08-29 18:15:53.141 6305-7075/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-27227
java.lang.OutOfMemoryError
at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:322)
at com.android.volley.toolbox.DiskBasedCache.readString(DiskBasedCache.java:540)
at com.android.volley.toolbox.DiskBasedCache.readStringStringMap(DiskBasedCache.java:562)
at com.android.volley.toolbox.DiskBasedCache$CacheHeader.readHeader(DiskBasedCache.java:403)
at com.android.volley.toolbox.DiskBasedCache.initialize(DiskBasedCache.java:156)
at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:84)
這裏是我的代碼:
public class DataReceiver {
public static void initializeCountriesListUpdate(String urlString, final Context context) throws IOException {
RequestQueue requestQueue = Volley.newRequestQueue(context);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlString,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
ArrayList<Country> countriesList = JsonParser.getCountriesFromJson(response, context);
((CountriesListActivity)context).updateCountriesList(countriesList);
((CountriesListActivity)context).progressDialog.hide();
((CountriesListActivity)context).progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("NetworkManager", "Cannot resolve data from the server");
}
});
VolleySingleton.getInstance().getRequestQueue().add(jsonObjectRequest);
}
UPD1:
我試着添加
android:hardwareAccelerated="false"
android:largeHeap="true"
到Manifest,但它沒有幫助。
你分析過你的堆轉儲嗎? – spiderman
我的問題是緩存缺失。感謝Udi提供鏈接提示。這個手冊幫助我make.my singleton正確:https://developer.android.com/training/volley/requestqueue.html#singleton –