每次连续调用Volley并在RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext())时出错时,我遇到内存不足异常。
像这样每5秒调用一次Method。
handlerGetJockyLatLong = new Handler();
runnableJockyLatLong = new Runnable() {
@Override
public void run() {
handlerGetJockyLatLong.postDelayed(runnableJockyLatLong, 10000);
getJockyLatLongFromBackEnd();
}
};
handlerGetJockyLatLong.postDelayed(runnableJockyLatLong, 10000);
方法是:
private void getJockyLatLongFromBackEnd() {
final String getJockyID_URL = getProfileInformationURL(getUserAccessToken(UserSideTrackingPage.this), UserID);
Log.e("getJockyID_URL", getJockyID_URL);
StringRequest request = new StringRequest(Request.Method.GET, getJockyID_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response != null && !response.startsWith("<HTML>")) {
Log.e("getJocky_Url_Responce", response);
//progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject jsonObbjectError = jsonObject.getJSONObject("error");
String errorCode = jsonObbjectError.getString("code");
String errorMessage = jsonObbjectError.getString("message");
if (errorCode.equals("0")) {
if (jsonObject.has("data")) {
JSONObject jsonObjectData = jsonObject.getJSONObject("data");
Double latitude = Double.valueOf(jsonObjectData.getString("latitude"));
Double longitude = Double.valueOf(jsonObjectData.getString("longitude"));
globalGoogleMap.clear();
currentLocationMarker = CommonUtils.createMultipleMarkers(globalGoogleMap, latitude, longitude, "Jocky Location", R.drawable.current_location);
PickupLocationMarker = CommonUtils.createMultipleMarkers(globalGoogleMap, Double.valueOf(pickupLat), Double.valueOf(pickupLong), "Pickup Location", R.drawable.pickup_marker_icon);
DropLocationMarker = CommonUtils.createMultipleMarkers(globalGoogleMap, Double.valueOf(dropLong), Double.valueOf(dropLat), "Drop Location", R.drawable.drop_location_marker_icon);
}
} else {
Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("Exception", e.toString());
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.toString());
}
});
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);
}
请告诉我我做错了吗?我们不应该总是打排球吗?还是我应该将凌空放在单例课程中,以便我只能实例化一次?任何建议都可以。谢谢。
最佳答案
尝试在下面使用static
RequestQueue,如下所示,我也遇到了同样的情况。当我使用static时,它对我有用。
public class name extends AppCompactActivity{
static RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
public void yourmethod{
//code here
queue.add(request);
}
}