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