Search in sources :

Example 1 with HttpException

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

the class HttpFragment method testUpload.

/////////////////////////////////////// other ////////////////////////////////////////////////////////////////
//@OnClick(R.id.download_btn)
public void testUpload(View view) {
    // 设置请求参数的编码
    //RequestParams params = new RequestParams("GBK");
    // 默认编码UTF-8
    RequestParams params = new RequestParams();
    //params.addQueryStringParameter("qmsg", "你好");
    //params.addBodyParameter("msg", "测试");
    // 添加文件
    params.addBodyParameter("file", new File("/sdcard/test.zip"));
    //params.addBodyParameter("testfile", new File("/sdcard/test2.zip")); // 继续添加文件
    // 用于非multipart表单的单文件上传
    //params.setBodyEntity(new FileUploadEntity(new File("/sdcard/test.zip"), "binary/octet-stream"));
    // 用于非multipart表单的流上传
    //params.setBodyEntity(new InputStreamUploadEntity(stream ,length));
    HttpUtils http = new HttpUtils();
    // 设置返回文本的编码, 默认编码UTF-8
    //http.configResponseTextCharset("GBK");
    // 自动管理 cookie
    http.configCookieStore(preferencesCookieStore);
    http.send(HttpRequest.HttpMethod.POST, "http://192.168.1.5:8080/UploadServlet", params, new RequestCallBack<String>() {

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

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

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            resultText.setText("reply: " + 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) File(java.io.File) RequestParams(com.lidroid.xutils.http.RequestParams)

Example 2 with HttpException

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

the class HttpFragment method testPost.

//@OnClick(R.id.download_btn)
public void testPost(View view) {
    RequestParams params = new RequestParams();
    params.addQueryStringParameter("method", "mkdir");
    params.addQueryStringParameter("access_token", "3.1042851f652496c9362b1cd77d4f849b.2592000.1377530363.3590808424-248414");
    params.addBodyParameter("path", "/apps/测试应用/test文件夹");
    HttpUtils http = new HttpUtils();
    http.send(HttpRequest.HttpMethod.POST, "https://pcs.baidu.com/rest/2.0/pcs/file", 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("upload 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) RequestParams(com.lidroid.xutils.http.RequestParams)

Example 3 with HttpException

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

the class SyncHttpHandler method sendRequest.

public ResponseStream sendRequest(HttpRequestBase request) throws HttpException {
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (true) {
        boolean retry = true;
        IOException exception = null;
        try {
            requestUrl = request.getURI().toString();
            requestMethod = request.getMethod();
            if (HttpUtils.sHttpCache.isEnabled(requestMethod)) {
                String result = HttpUtils.sHttpCache.get(requestUrl);
                if (result != null) {
                    return new ResponseStream(result);
                }
            }
            HttpResponse response = client.execute(request, context);
            return handleResponse(response);
        } catch (UnknownHostException e) {
            exception = e;
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        } catch (IOException e) {
            exception = e;
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        } catch (NullPointerException e) {
            exception = new IOException(e.getMessage());
            exception.initCause(e);
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        } catch (HttpException e) {
            throw e;
        } catch (Throwable e) {
            exception = new IOException(e.getMessage());
            exception.initCause(e);
            retry = retryHandler.retryRequest(exception, ++retriedTimes, context);
        }
        if (!retry) {
            throw new HttpException(exception);
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpResponse(org.apache.http.HttpResponse) HttpException(com.lidroid.xutils.exception.HttpException) IOException(java.io.IOException) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler)

Example 4 with HttpException

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

the class SyncHttpHandler method handleResponse.

private ResponseStream handleResponse(HttpResponse response) throws HttpException, IOException {
    if (response == null) {
        throw new HttpException("response is null");
    }
    StatusLine status = response.getStatusLine();
    int statusCode = status.getStatusCode();
    if (statusCode < 300) {
        ResponseStream responseStream = new ResponseStream(response, charset, requestUrl, expiry);
        responseStream.setRequestMethod(requestMethod);
        return responseStream;
    } 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) DefaultHttpRedirectHandler(com.lidroid.xutils.http.callback.DefaultHttpRedirectHandler) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpException(com.lidroid.xutils.exception.HttpException)

Example 5 with HttpException

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

the class HttpHandler method sendRequest.

// 执行请求
@SuppressWarnings("unchecked")
private ResponseInfo<T> sendRequest(HttpRequestBase request) throws HttpException {
    HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
    while (true) {
        if (autoResume && isDownloadingFile) {
            File downloadFile = new File(fileSavePath);
            long fileLen = 0;
            if (downloadFile.isFile() && downloadFile.exists()) {
                fileLen = downloadFile.length();
            }
            if (fileLen > 0) {
                request.setHeader("RANGE", "bytes=" + fileLen + "-");
            }
        }
        boolean retry = true;
        IOException exception = null;
        try {
            requestMethod = request.getMethod();
            if (HttpUtils.sHttpCache.isEnabled(requestMethod)) {
                String result = HttpUtils.sHttpCache.get(requestUrl);
                if (result != null) {
                    return new ResponseInfo<T>(null, (T) result, true);
                }
            }
            ResponseInfo<T> responseInfo = null;
            if (!isCancelled()) {
                HttpResponse response = client.execute(request, context);
                responseInfo = handleResponse(response);
            }
            return responseInfo;
        } catch (UnknownHostException e) {
            exception = e;
            retry = retryHandler.retryRequest(exception, ++retriedCount, context);
        } catch (IOException e) {
            exception = e;
            retry = retryHandler.retryRequest(exception, ++retriedCount, context);
        } catch (NullPointerException e) {
            exception = new IOException(e.getMessage());
            exception.initCause(e);
            retry = retryHandler.retryRequest(exception, ++retriedCount, context);
        } catch (HttpException e) {
            throw e;
        } catch (Throwable e) {
            exception = new IOException(e.getMessage());
            exception.initCause(e);
            retry = retryHandler.retryRequest(exception, ++retriedCount, context);
        }
        if (!retry) {
            throw new HttpException(exception);
        }
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpException(com.lidroid.xutils.exception.HttpException) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) File(java.io.File)

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