首页 > 代码库 > android-async-http 请求分析<原创>

android-async-http 请求分析<原创>

android-async-http发送请求时,真正执行请求的地方是在AsyncHttpRequest类中的,有两个方法:

 private void makeRequest() throws IOException {
        if (isCancelled()) {
            return;
        }
        // Fixes #115
        if (request.getURI().getScheme() == null) {
            // subclass of IOException so processed in the caller
            throw new MalformedURLException("No valid URI scheme was provided");
        }

        HttpResponse response = client.execute(request, context);

        if (!isCancelled() && responseHandler != null) {
            responseHandler.sendResponseMessage(response);
        }
    }

    private void makeRequestWithRetries() throws IOException {
        boolean retry = true;
        IOException cause = null;
        HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
        try {
            while (retry) {
                try {
                    makeRequest();
                    return;
                } catch (UnknownHostException e) {
                    // switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
                    // while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
                    // (to assist in genuine cases of unknown host) which seems better than outright failure
                    cause = new IOException("UnknownHostException exception: " + e.getMessage());
                    retry = (executionCount > 0) && retryHandler.retryRequest(cause, ++executionCount, context);
                } catch (NullPointerException e) {
                    // there‘s a bug in HttpClient 4.0.x that on some occasions causes
                    // DefaultRequestExecutor to throw an NPE, see
                    // http://code.google.com/p/android/issues/detail?id=5255
                    cause = new IOException("NPE in HttpClient: " + e.getMessage());
                    retry = retryHandler.retryRequest(cause, ++executionCount, context);
                } catch (IOException e) {
                    if (isCancelled()) {
                        // Eating exception, as the request was cancelled
                        return;
                    }
                    cause = e;
                    retry = retryHandler.retryRequest(cause, ++executionCount, context);
                }
                if (retry && (responseHandler != null)) {
                    responseHandler.sendRetryMessage(executionCount);
                }
            }
        } catch (Exception e) {
            // catch anything else to ensure failure message is propagated
            Log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
            cause = new IOException("Unhandled exception: " + e.getMessage());
        }

        // cleaned up to throw IOException
        throw (cause);
    }
makeRequest,就是发送请求的方法。HttpResponse response = client.execute(request, context);这一句是对请求的执行,response就是请求返回的结果。理论上来说,这一次就完成了请求,那么为什么还要有
makeRequestWithRetries()方法的存在?
原因是:HttpResponse response = client.execute(request, context)的执行有时候会失败,可能是网络卡了或者是其他原因,所以要对此方法进行多次重试。这个方法的核心就在于此。
如果请求成功了,那么直接return,否则则会进入异常(client.execute(request, context)抛出的....)异常的处理中改变了retry(一个boolean变量,是进行循环发送请求的循环判断条件),
cause = new IOException("NPE in HttpClient: " + e.getMessage());
retry = retryHandler.retryRequest(cause, ++executionCount, context);
retryHandler.retryRequest(cause, ++executionCount, context);本方法是apach包中提供的方法,需要查看android api。
api中对retryHandler的介绍是:

简单介绍就是判断是否要重试,具体的判断方法没有找到....