Search in sources :

Example 6 with HttpException

use of com.lidroid.xutils.exception.HttpException in project xUtils by wyouflf.

the class HttpHandler method handleResponse.

@SuppressWarnings("unchecked")
private ResponseInfo<T> handleResponse(HttpResponse response) throws HttpException, IOException {
    if (response == null) {
        throw new HttpException("response is null");
    }
    if (isCancelled())
        return null;
    StatusLine status = response.getStatusLine();
    int statusCode = status.getStatusCode();
    if (statusCode < 300) {
        Object result = null;
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            isUploading = false;
            if (isDownloadingFile) {
                autoResume = autoResume && OtherUtils.isSupportRange(response);
                String responseFileName = autoRename ? OtherUtils.getFileNameFromHttpResponse(response) : null;
                FileDownloadHandler downloadHandler = new FileDownloadHandler();
                result = downloadHandler.handleEntity(entity, this, fileSavePath, autoResume, responseFileName);
            } else {
                StringDownloadHandler downloadHandler = new StringDownloadHandler();
                result = downloadHandler.handleEntity(entity, this, charset);
                if (HttpUtils.sHttpCache.isEnabled(requestMethod)) {
                    HttpUtils.sHttpCache.put(requestUrl, (String) result, expiry);
                }
            }
        }
        return new ResponseInfo<T>(response, (T) result, false);
    } else if (statusCode == 301 || statusCode == 302) {
        if (httpRedirectHandler == null) {
            httpRedirectHandler = new DefaultHttpRedirectHandler();
        }
        HttpRequestBase request = httpRedirectHandler.getDirectRequest(response);
        if (request != null) {
            return this.sendRequest(request);
        }
    } else if (statusCode == 416) {
        throw new HttpException(statusCode, "maybe the file has downloaded completely");
    } else {
        throw new HttpException(statusCode, status.getReasonPhrase());
    }
    return null;
}
Also used : StatusLine(org.apache.http.StatusLine) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) HttpException(com.lidroid.xutils.exception.HttpException)

Example 7 with HttpException

use of com.lidroid.xutils.exception.HttpException in project xUtils by wyouflf.

the class HttpFragment method testGet.

//@OnClick(R.id.download_btn)
public void testGet(View view) {
    //RequestParams params = new RequestParams();
    //params.addHeader("name", "value");
    //params.addQueryStringParameter("name", "value");
    HttpUtils http = new HttpUtils();
    http.configCurrentHttpCacheExpiry(1000 * 10);
    http.send(HttpRequest.HttpMethod.GET, "http://www.baidu.com", //params,
    new RequestCallBack<String>() {

        @Override
        public void onStart() {
            resultText.setText("conn...");
        }

        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            resultText.setText(current + "/" + total);
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            resultText.setText("response:" + responseInfo.result);
        }

        @Override
        public void onFailure(HttpException error, String msg) {
            resultText.setText(msg);
        }
    });
}
Also used : HttpUtils(com.lidroid.xutils.HttpUtils) HttpException(com.lidroid.xutils.exception.HttpException)

Aggregations

HttpException (com.lidroid.xutils.exception.HttpException)7 HttpUtils (com.lidroid.xutils.HttpUtils)3 RequestParams (com.lidroid.xutils.http.RequestParams)2 File (java.io.File)2 IOException (java.io.IOException)2 UnknownHostException (java.net.UnknownHostException)2 HttpResponse (org.apache.http.HttpResponse)2 StatusLine (org.apache.http.StatusLine)2 HttpRequestRetryHandler (org.apache.http.client.HttpRequestRetryHandler)2 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)2 DefaultHttpRedirectHandler (com.lidroid.xutils.http.callback.DefaultHttpRedirectHandler)1 HttpEntity (org.apache.http.HttpEntity)1