【问题标题】:Getting outofmemoryException when calling Volley continuousally连续调用 Volley 时出现 outofmemoryException
【发布时间】: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


【解决方案1】:

尝试在这里使用staticRequestQueue,如下所示,我也遇到了同样的情况。当我使用静态时,它对我有用。

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

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

}

【讨论】:

  • #Omar 静态不能放入方法中。我需要把那句话放在课堂上吗?
  • 只需在方法外创建一个静态 RequestQueue 对象。并尝试调用对象..
  • 你不能像这样在常规类中调用应用程序上下文
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 2012-10-05
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
相关资源
最近更新 更多