连续调用Volley时出现outofmemoryException

时间:2017-05-13 11:11:32

标签: java android exception out-of-memory android-volley

每次在连续调用volley并在RequestQueue上获取错误时我得到了outofmemory异常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);
}

请告诉我,我做错了?我们不应该总是叫凌空吗?或者我应该将截击放在单身类中,这样我只能实例化一次?任何建议都没问题。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试在这里使用static RequestQueue,如下所示,我也经历了同样的事情。当我使用静态时,它对我有效。

    public class name extends AppCompactActivity{
        static RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

    public void yourmethod{
    //code here
    queue.add(request);
    }

}