2017-07-06 70 views
1

我很感激您可以给我的任何帮助。我有以下问题:Android凌空 - 当所有请求完成时得到回调

我有一个应用程序使用SyncAdapter定期和手动同步本地数据库与服务器数据。但是我需要当用户刚刚登录到应用程序时,执行手动同步,同时显示一个加载对话框。同步完成后,对话框应隐藏,并显示主要活动。你推荐什么?

我正在使用volley处理HTTP请求。我有点困惑,因为请求总是异步运行,所以很难知道所有请求何时完成以隐藏对话。

的代码如下:

VolleySingleton.getInstance(getContext()).addToRequestQueue(
     new JsonObjectRequest(
      Request.Method.GET, 
      Constants.GET_URL, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        // This method sync the data 
        updateLocalData(response, syncResult); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        getContext().sendBroadcast(new Intent(Constants.SYNC_CORRUPTED_BY_SERVER)); 
        Log.d(TAG, "Sync (makeLocalSync), Exception => " + 
          error.getLocalizedMessage()); 
       } 
      } 
     ) 
    ); 

对不起,我的英语水平...

谢谢!

+0

请考虑稍后有“n”个请求。其他“n”VolleySingleton.getInstance(getContext())。 AddToRequestQueue(... –

回答

2

不幸的是,Volley的RequestQueue不提供任何界面或回调,以便在所有待处理请求完成时通知您。我发现在所有请求完成时通知的解决方法是为每个请求的响应或失败更新一个int和一个布尔类成员。请求有权访问这些成员并且该数字与更新后的值相对应,这一点非常重要,因此当此值降至0时,您将知道所有请求都已完成。

private int numberOfRequestsToMake = NUMBER; 
private boolean hasRequestFailed = false; 

然后,创建请求时:

JsonObjectRequest req = new JsonObjectRequest(url, null, 
      new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, "successful request to " + url); 
        numberOfRequestsToMake--; 

        if(numberOfRequestsToMake == 0) { 
         if(!hasRequestFailed) { 
          //All requests finished correctly 
         } else { 
          //At least one request failed 
         } 
        } 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e(TAG, "unsuccessful request to " + url); 
        numberOfRequestsToMake--; 
        hasRequestFailed = true; 

        if(numberOfRequestsToMake == 0) { 
         //The last request failed 
        } 
       } 
      } 
    ); 

    MyVolleySingleton.getInstance().addToRequestQueue(req); 
-1

我已经解决了我的问题如下:

在每一个 “N” 排球的请求,他们的方法Response.Listener内Respones.ErrorListener我使用计数器标志,允许我吨o处理广播,如下所示:

如果计数器的值等于请求数,则所有请求都已完成,然后用该标志检查是否有任何请求出现问题。

的代码如下:

请求#N1:

// Counter and flag to handle requests 
    private static int iCountRequest; 
    private static boolean bAnyErrors; 
    // Before initiating all the requests, the variables must be restarted 
    iCountRequest = 0; 
    bAnyErrors = false; 
    // Request # n1: 
    VolleySingleton.getInstance(getContext()).addToRequestQueue(
      new JsonObjectRequest(
        Request.Method.GET, 
        Constants.REQUEST_N1_URL, 
        new Response.Listener<JSONObject>() { 
         @Override 
         public void onResponse(JSONObject response) { 
          iCountRequest++; 
          // Response code.. 
          sendBroadcastToActivity(); 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError error) { 
          iCountRequest++; 
          bAnyErrors = true; 
          sendBroadcastToActivity(); 
         } 
        } 
      ) 
    ); 

sendBroadCast功能:

if (iCountRequest == Constants.NUMBER_OF_REQUEST) { 
     if (!bAnyErrors) { 
      getContext().sendBroadcast(new Intent(Constants 
        .SYNC_IN_ORDER_FINISHED)); 
     } else { 
      getContext().sendBroadcast(new Intent(Constants 
        .SYNC_IN_ORDER_FINISHED_WITH_ERRORS)); 
     } 
    } 

VolleySingleton:

public final class VolleySingleton { 

    private static VolleySingleton oSingleton; 
    private RequestQueue oRQ; 
    private static Context oContext; 

    private VolleySingleton(Context context) { 
     VolleySingleton.oContext = context; 
     oRQ = getRequestQueue(); 
    } 

    public static synchronized VolleySingleton getInstance(Context context) { 
     if (oSingleton == null) { 
      oSingleton = new VolleySingleton(context.getApplicationContext()); 
     } 
     return oSingleton; 
    } 

    private RequestQueue getRequestQueue() { 
     if (oRQ == null) { 
      oRQ = Volley.newRequestQueue(oContext.getApplicationContext()); 
     } 
     return oRQ; 
    } 

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

} 

我希望对你有用。谢谢@FerDensetsu