Volley异常的介绍

Posted ihrthk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Volley异常的介绍相关的知识,希望对你有一定的参考价值。

异常种类(VolleyError)

ServerError的情况

1.entitiy为空

InputStream in = entity.getContent();
if (in == null)
throw new ServerError();

2.返回5xx

// TODO: Only throw ServerError for 5xx status codes.
throw new ServerError(networkResponse);

TimeoutError

1.SocketTimeoutException

attemptRetryOnException(“socket”, request, new TimeoutError());

2.ConnectTimeoutException

attemptRetryOnException(“connection”, request, new TimeoutError());

RedirectError

statusCode == HttpStatus.SC_MOVED_PERMANENTLY ||
statusCode == HttpStatus.SC_MOVED_TEMPORARILY

ParseError

/**
     * Subclasses must implement this to parse the raw network response
     * and return an appropriate response type. This method will be
     * called from a worker thread.  The response will not be delivered
     * if you return null.
     * @param response Response from the network
     * @return The parsed response, or null in the case of an error
     */
    abstract protected Response<T> parseNetworkResponse(NetworkResponse response);

AuthFailureError

/**
 * An Authenticator that uses @link AccountManager to get auth
 * tokens of a specified type for a specified account.
 */
public class androidAuthenticator implements Authenticator
  // TODO: Figure out what to do about notifyAuthFailure
@SuppressWarnings("deprecation")
@Override
public String getAuthToken() throws AuthFailureError 
    AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount,
            mAuthTokenType, mNotifyAuthFailure, null, null);
    Bundle result;
    try 
        result = future.getResult();
     catch (Exception e) 
        throw new AuthFailureError("Error while retrieving auth token", e);
    
    String authToken = null;
    if (future.isDone() && !future.isCancelled()) 
        if (result.containsKey(AccountManager.KEY_INTENT)) 
            Intent intent = result.getParcelable(AccountManager.KEY_INTENT);
            throw new AuthFailureError(intent);
        
        authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
    
    if (authToken == null) 
        throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType);
    

    return authToken;

if (responseContents != null) 
    networkResponse = new NetworkResponse(statusCode, responseContents,
            responseHeaders, false, SystemClock.elapsedRealtime() - requestStart);
    if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
            statusCode == HttpStatus.SC_FORBIDDEN) 
        attemptRetryOnException("auth",
                request, new AuthFailureError(networkResponse));
     else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || 
                statusCode == HttpStatus.SC_MOVED_TEMPORARILY) 
        attemptRetryOnException("redirect",
                request, new RedirectError(networkResponse));
     else 
        // TODO: Only throw ServerError for 5xx status codes.
        throw new ServerError(networkResponse);
    
 else 
    throw new NetworkError(e);

NetworkError

httpResponse = mHttpStack.performRequest(request, headers);

throw IOException

NoNConnectionException

NetworkResponse networkResponse = null;
if (httpResponse != null) 
    statusCode = httpResponse.getStatusLine().getStatusCode();
 else 
    throw new NoConnectionError(e);

以上是关于Volley异常的介绍的主要内容,如果未能解决你的问题,请参考以下文章

volley介绍08

volley介绍03

volley介绍06

使用Volley上传头像图片

volley_缓存介绍

理解Volley -- Android 学习之路