【发布时间】:2017-05-13 11:11:32
【问题描述】:
每次连续调用 volley 并在 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 处出现错误时,我都会遇到内存不足异常;
像这样每 5 秒调用一次方法。
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);
}
请告诉我我做错了吗?我们不应该一直叫凌空吗?还是应该将 volley 放在单例类中,这样我只能实例化一次?任何建议都可以。谢谢。
【问题讨论】:
-
你尝试过使用单例吗?
标签: java android exception out-of-memory android-volley