【问题标题】:Android volley multiple request. debug work fine, run app work wrongAndroid凌空多个请求。调试工作正常,运行应用程序工作错误
【发布时间】:2016-03-16 20:54:48
【问题描述】:

在没有用户参与的情况下尝试一个接一个地执行请求时遇到问题。 运行应用程序时,我可以看到此日志

Posting params: {email=xxxxx@gmail.com, tag=getConnectors}
    verifyOperation onResponse --> 1 operation
    verifyOperation onResponse --> {"result":"85","msg":"ok, user register, your password is your number phone!"}                                                                                              
  response 1 operation is ok call -->getConnectors
Posting params: {email=xxxxx@gmail.com, tag=geinfodates}
verifyOperation onResponse --> 2 operation
verifyOperation onResponse -->{"result":"85","msg":"ok, user register, your password is your number phone!"} 

但是在调试app的时候,2操作的结果是正确的:

Posting params: {email=xxxxx@gmail.com, tag=geinfodates}
verifyOperation onResponse --> 2
verifyOperation onResponse -->{"token":"xxxxxxxx"}   

我有一个单例类,其中有我的 StringRequest
拥有不同类型的听众

 void verifyOperation(final HashMap paramOperation, final int opcion,final String url) {
            pref = new PrefManager(context);
            String urlFinale=url;
            StringRequest strReq = new StringRequest(Request.Method.POST,
                    urlFinale, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
 switch (opcion) {

                        case 1:
                           if (operation.result >= 0)
                           break;
                        case :2  .....

},新的 Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
            Toast.makeText(context,
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            HashMap<String, String> profile = new HashMap<>();
            Set set = paramOperation.entrySet();
            Iterator iterator = set.iterator();
            //String number=pref.getMobileNumber();
            String mail = pref.getMail();
            switch (opcion) {
                case 1:
                    break;
                case 2:
                    profile = pref.getUserDetails();
                 break;

}

最后将我的 stringrequest 添加到我的单例类中以控制 volley request

// Adding request to request queue
        MyApplication.getInstance().addToRequestQueue(strReq);



public <T> void addToRequestQueue(Request<T> req) {
            req.setTag(TAG);
            req.setRetryPolicy(new DefaultRetryPolicy(
                    MY_SOCKET_TIMEOUT_MS,
                    -1, //intentos
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            getRequestQueue().add(req);
        }

我玩过默认参数RetryPolicy,但总是失败。 我不明白,因为我的第二个请求的答案可能只是第一个的答案,要调用第二个操作,我需要通过第一个 onresponse 第二次调用永远不会返回与第一次相同的数据,正如我在调试时在日志中看到的那样

谢谢!

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    我不明白,因为有人给我投了反对票...

    我解决了我的问题。也许可以帮助我解决某人。 Volley 有缓存 将请求添加到队列时,可以禁用缓存 https://developer.android.com/intl/es/training/volley/simple.html 出于这个原因,当我调试时,我的应用程序工作正常,缓存应该在 time(1-2 seconds into requests) 之前被删除,当我运行我的应用程序时,缓存工作并总是返回第一个响应。 您可以禁用缓存,这是一个示例 Disable Volley cache management

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 2014-11-30
      • 1970-01-01
      • 2023-04-04
      • 2020-11-01
      • 1970-01-01
      • 2017-07-01
      • 2014-06-08
      • 1970-01-01
      相关资源
      最近更新 更多