在android中使用volley调用web api时出现内存不足错误
我创建了一个名为在android中使用volley调用web api时出现内存不足错误,android,android-volley,Android,Android Volley,我创建了一个名为BaseActivity.java的通用类,并在其中定义了volley函数。此外,我还将BaseActivity类扩展到每个活动,并使用方法调用web api。 代码如下所示 public void callVolley(final Context cContext, String mMethodType, final String mMethodname, String URL, final HashMap<Strin
BaseActivity.java
的通用类,并在其中定义了volley函数。此外,我还将BaseActivity
类扩展到每个活动,并使用方法调用web api。
代码如下所示
public void callVolley(final Context cContext, String mMethodType, final String mMethodname, String URL,
final HashMap<String, String> mMap, int initialTimeoutMs, boolean shouldCache, int maxNumRetries,
Boolean isProgressDailogEnable, Boolean isCancelable, final Activity aActivity) {
// TODO Auto-generated method stub
mMap.put("deviceType", "1");
SharedPreferences setting = getSharedPreferences(Constant.PREFRENCE_NAME, MODE_PRIVATE);
mMap.put("loginUserId", setting.getString("userId", "") + "");
mMap.put("deviceToken", "" + setting.getString("deviceToken", ""));
mMap.put("deviceId", setting.getString("deviceId", "") + "");
if (Constant.d) Log.i(mMethodname + "==>", mMap.toString() + "");
if (!isOnline(cContext)) {
showErrorDailog(aActivity, Constant.PleaseCheckInternetConnection, R.drawable.icon);
} else {
StringRequest jsObjRequest;
int reqType = 0;
String RequestURL = URL.trim();
queue = Volley.newRequestQueue(cContext);
if (isProgressDailogEnable) {
customLoaderDialog = new CustomLoaderDialog(cContext);
customLoaderDialog.show(isCancelable);
customLoaderDialog.dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// finish();
}
});
}
if (mMethodType.trim().equalsIgnoreCase("GET"))
reqType = com.android.volley.Request.Method.GET;
else if (mMethodType.trim().equalsIgnoreCase("POST"))
reqType = com.android.volley.Request.Method.POST;
if (RequestURL.equals(""))
RequestURL = Constant.WEBSERVICE_URL;
else
RequestURL = URL;
if (Constant.d) Log.d("reqType", reqType + "");
jsObjRequest = new StringRequest(reqType, RequestURL, new com.android.volley.Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (Constant.d) Log.d("response==>" + mMethodname, "" + response);
if (customLoaderDialog != null) {
try {
customLoaderDialog.hide();
} catch (Exception e) {
e.printStackTrace();
}
}
if (response == null || response.length() == 0) {
IVolleyRespose iVolleyRespose = (IVolleyRespose) aActivity;
iVolleyRespose.onVolleyResponse(RESPONSE_ERROR, response, mMethodname);
} else {
JSONObject json_str;
try {
json_str = new JSONObject(response);
int status = json_str.getInt("status");
if (status == 101) {
AlertDialog alertDialog = new AlertDialog.Builder(aActivity).create();
alertDialog.setTitle(Constant.Alert_Name);
alertDialog.setMessage(json_str.getString("message") + "");
alertDialog.setCancelable(false);
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
SharedPreferences setting = getSharedPreferences(Constant.PREFRENCE_NAME, MODE_PRIVATE);
setting.edit().clear().commit();
ExitActivity.exitApplication(aActivity);
finish();
System.exit(0);
}
});
alertDialog.show();
} else {
IVolleyRespose iVolleyRespose = (IVolleyRespose) aActivity;
iVolleyRespose.onVolleyResponse(RESPONSE_OK, response, mMethodname);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
// TODO Auto-generated method stub
IVolleyRespose iVolleyError = (IVolleyRespose) aActivity;
iVolleyError.onVolleyError(RESPONSE_ERROR, "Error", mMethodname);
if (customLoaderDialog != null) {
customLoaderDialog.hide();
}
}
}) {
@Override
protected Map<String, String> getParams() {
String strRequest = "";
try {
strRequest = getWebservicejsObjRequestforvolley(mMethodname, mMap);
if (Constant.d) Log.d("Request==>", strRequest + "");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> params = new HashMap<>();
params.put("json", strRequest);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
//if(Constant.d) Log.d("Request==>", jsObjRequest+"");
jsObjRequest.setTag(mMethodname);
jsObjRequest.setShouldCache(shouldCache);
jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(initialTimeoutMs, maxNumRetries, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsObjRequest);
}
}
问题是当我使用应用程序5到10分钟时,我找不到为什么这个错误会随机发生。
如果任何一方面临此类错误,则建议采用解决方案。临时解决方案,但不推荐 在清单文件中添加此行
<application
android:largeHeap="true"
将largeheap属性添加到清单文件中的应用程序标记,如下所示:
android:largeHeap="true"
为什么每次创建一个requestqueue时都要重复使用它?分析你的转储,什么东西在泄漏?@ankitagrawal:这段代码在基类中。我在每个活动中扩展了它,所以每个活动我都让requestqueue传递该活动的上下文
android:largeHeap="true"