Volley异常的介绍

异常种类(VolleyError)

屏幕快照 2015-12-20 15.43.08.png-46.7kB

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值