簡體   English   中英

在Volley中重新登錄並重試請求

[英]Relogin and retry request in Volley

如果我從服務器收到401錯誤,則需要執行自動重新登錄和重試請求(如果成功)。 我正在使用Volley lib。 據我從Volley源代碼所看到的,它在while(true)循環中運行,直到得到響應或異常:

@Override
public NetworkResponse performRequest(Request<?> request){
    while (true) {
        try {
            httpResponse = mHttpStack.performRequest(request, headers);
            return httpResponse;
        } catch (IOException e) {
            statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                attemptRetryOnException("auth", request, new AuthFailureError(networkResponse));
            }
        }
    }
}

但我想停止此循環,直到獲得成功的登錄響應,然后繼續嘗試獲取響應。

是否可以在自定義RetryPolicy中進行任何操作而無需在Volley源代碼中進行修改?

實現自己的RetryPolicy並重寫public void retry(VolleyError error)方法,如下所示:

    @Override
    public void retry(VolleyError error) throws VolleyError {
        if (error.networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED)
        {
            throw new VolleyError("Client is not authorized, retry is pointless");
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM