Search in sources :

Example 1 with FileRequest

use of com.kymjs.rxvolley.client.FileRequest in project yoo_home_Android by culturer.

the class RxVolley method download.

/**
 * 下载
 *
 * @param storeFilePath    本地存储绝对路径
 * @param url              要下载的文件的url
 * @param progressListener 下载进度回调
 * @param callback         回调
 */
public static FileRequest download(String storeFilePath, String url, ProgressListener progressListener, HttpCallback callback) {
    RequestConfig config = new RequestConfig();
    config.mUrl = url;
    config.mRetryPolicy = new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 20, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    FileRequest request = new FileRequest(storeFilePath, config, callback);
    request.setTag(url);
    request.setOnProgressListener(progressListener);
    new Builder().setRequest(request).doTask();
    return request;
}
Also used : RequestConfig(com.kymjs.rxvolley.client.RequestConfig) FileRequest(com.kymjs.rxvolley.client.FileRequest) DefaultRetryPolicy(com.kymjs.rxvolley.http.DefaultRetryPolicy)

Example 2 with FileRequest

use of com.kymjs.rxvolley.client.FileRequest in project yoo_home_Android by culturer.

the class Network method performRequest.

/**
 * 实际执行一个请求的方法
 *
 * @param request 一个请求任务
 * @return 一个不会为null的响应
 * @throws VolleyError
 */
@Override
public NetworkResponse performRequest(Request<?> request) throws VolleyError {
    while (true) {
        URLHttpResponse httpResponse = null;
        byte[] responseContents = null;
        HashMap<String, String> responseHeaders = new HashMap<>();
        try {
            // 标记Http响应头在Cache中的tag
            ArrayList<HttpParamsEntry> headers = new ArrayList<>();
            addCacheHeaders(headers, request.getCacheEntry());
            httpResponse = mHttpStack.performRequest(request, headers);
            int statusCode = httpResponse.getResponseCode();
            responseHeaders = httpResponse.getHeaders();
            if (statusCode == HttpStatus.SC_NOT_MODIFIED) {
                // 304
                return new NetworkResponse(HttpStatus.SC_NOT_MODIFIED, request.getCacheEntry() == null ? null : request.getCacheEntry().data, responseHeaders, true);
            }
            if (httpResponse.getContentStream() != null) {
                if (request instanceof FileRequest) {
                    responseContents = ((FileRequest) request).handleResponse(httpResponse);
                } else {
                    responseContents = entityToBytes(httpResponse);
                }
            } else {
                responseContents = new byte[0];
            }
            if (statusCode < 200 || statusCode > 299) {
                throw new IOException();
            }
            return new NetworkResponse(statusCode, responseContents, responseHeaders, false);
        } catch (SocketTimeoutException e) {
            attemptRetryOnException("socket", request, new VolleyError(new SocketTimeoutException("socket timeout")));
        } catch (MalformedURLException e) {
            attemptRetryOnException("connection", request, new VolleyError("Bad URL " + request.getUrl(), e));
        } catch (IOException e) {
            int statusCode;
            NetworkResponse networkResponse;
            if (httpResponse != null) {
                statusCode = httpResponse.getResponseCode();
            } else {
                throw new VolleyError("NoConnection error", e);
            }
            Log.d("RxVolley", String.format(Locale.getDefault(), "Unexpected response code %d for %s", statusCode, request.getUrl()));
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents, responseHeaders, false);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN) {
                    attemptRetryOnException("auth", request, new VolleyError(networkResponse));
                } else {
                    throw new VolleyError(networkResponse);
                }
            } else {
                throw new VolleyError(String.format(Locale.getDefault(), "Unexpected response code %d for %s", statusCode, request.getUrl()));
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry) SocketTimeoutException(java.net.SocketTimeoutException) FileRequest(com.kymjs.rxvolley.client.FileRequest)

Aggregations

FileRequest (com.kymjs.rxvolley.client.FileRequest)2 RequestConfig (com.kymjs.rxvolley.client.RequestConfig)1 DefaultRetryPolicy (com.kymjs.rxvolley.http.DefaultRetryPolicy)1 HttpParamsEntry (com.kymjs.rxvolley.toolbox.HttpParamsEntry)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1