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);
}
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()));
}
}
}
}
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)));
}
}
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);
}
}
Aggregations