如何在没有outofmemoryerror的情况下持续调用webservice?

时间:2017-05-23 12:07:37

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

我已经使用了凌空作为后台任务而且我的内存异常。这是我的代码

我所做的是使用像

这样的处理程序每​​10秒调用一次方法
        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), Other_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>")) {
              }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Error", error.toString());
        }
    });

    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    queue.add(request);
}

我出现了错误并且在这里崩溃

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

请告诉我我必须做什么。在这种情况下,我必须每次响应时更新UI。

1 个答案:

答案 0 :(得分:0)

调用网址时尝试此操作:

 try {
            NetworkController.getInstance().getRequestQueue().getCache().get(getJockyID_URL);
        }catch (Exception e){
            e.printStackTrace();
        }

Networkcontroller.class:

public class NetworkController extends Application {

private static final String TAG = NetworkController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static NetworkController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized NetworkController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}

}