Search in sources :

Example 6 with HttpParamsEntry

use of com.kymjs.rxvolley.toolbox.HttpParamsEntry in project yoo_home_Android by culturer.

the class HttpConnectStack method performRequest.

@Override
public URLHttpResponse performRequest(Request<?> request, ArrayList<HttpParamsEntry> additionalHeaders) throws IOException {
    String url = request.getUrl();
    ArrayList<HttpParamsEntry> header = new ArrayList<>();
    header.addAll(request.getHeaders());
    header.addAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (HttpParamsEntry entry : header) {
        connection.addRequestProperty(entry.k, entry.v);
    }
    setConnectionParametersForRequest(connection, request);
    return responseFromConnection(connection);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry) URL(java.net.URL)

Example 7 with HttpParamsEntry

use of com.kymjs.rxvolley.toolbox.HttpParamsEntry 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)

Example 8 with HttpParamsEntry

use of com.kymjs.rxvolley.toolbox.HttpParamsEntry in project yoo_home_Android by culturer.

the class Network method addCacheHeaders.

/**
 * 标记Respondeader响应头在Cache中的tag
 */
private void addCacheHeaders(ArrayList<HttpParamsEntry> headers, ICache.Entry entry) {
    if (entry == null) {
        return;
    }
    if (entry.etag != null) {
        headers.add(new HttpParamsEntry("If-None-Match", entry.etag));
    }
    if (entry.serverDate > 0) {
        Date refTime = new Date(entry.serverDate);
        DateFormat sdf = SimpleDateFormat.getDateTimeInstance();
        headers.add(new HttpParamsEntry("If-Modified-Since", sdf.format(refTime)));
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry) Date(java.util.Date)

Example 9 with HttpParamsEntry

use of com.kymjs.rxvolley.toolbox.HttpParamsEntry in project yoo_home_Android by culturer.

the class Request method encodeParameters.

/**
 * 对中文参数做URL转码
 */
private byte[] encodeParameters(ArrayList<HttpParamsEntry> params, String paramsEncoding) {
    StringBuilder encodedParams = new StringBuilder();
    try {
        for (HttpParamsEntry entry : params) {
            encodedParams.append(URLEncoder.encode(entry.k, paramsEncoding));
            encodedParams.append('=');
            encodedParams.append(URLEncoder.encode(entry.v, paramsEncoding));
            encodedParams.append('&');
        }
        return encodedParams.toString().getBytes(paramsEncoding);
    } catch (UnsupportedEncodingException uee) {
        throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry)

Aggregations

HttpParamsEntry (com.kymjs.rxvolley.toolbox.HttpParamsEntry)9 Request (com.kymjs.rxvolley.http.Request)2 URLHttpResponse (com.kymjs.rxvolley.http.URLHttpResponse)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 FileRequest (com.kymjs.rxvolley.client.FileRequest)1 Call (com.squareup.okhttp.Call)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Response (com.squareup.okhttp.Response)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1