package com.shinylife.smalltools.helper;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpClientHelper {
private static final long serialVersionUID = 808018030183407996L;
private static final int OK = 200;// OK: Success!
private static final int NOT_MODIFIED = 304;
private static final int BAD_REQUEST = 400;
private static final int NOT_AUTHORIZED = 401;
private static final int FORBIDDEN = 403;
private static final int NOT_FOUND = 404;
private static final int NOT_ACCEPTABLE = 406;
private static final int INTERNAL_SERVER_ERROR = 500;
private static final int BAD_GATEWAY = 502;
private static final int SERVICE_UNAVAILABLE = 503;
private final static boolean DEBUG = Constants.DEBUG;
private int retryCount = Constants.HTTP_REQUEST_RETRY_COUNT;
private int retryIntervalMillis = Constants.HTTP_REQUEST_RETRY_INTERVAL_SECONDS * 1000;
private int connectionTimeout = Constants.HTTP_REQUEST_CONNECTION_TIMEOUT * 1000;
private int readTimeout = Constants.HTTP_REQUEST_READ_TIMEOUT * 1000;
private String reqestCharset = "utf-8";
private String responseCharset = "utf-8";
private Map<String, String> requestHeaders = new HashMap<String, String>();
public void setRequestCharset(String charset) {
reqestCharset = charset;
}
public String getRequestCharset() {
return reqestCharset == null ? "ISO-8859-1" : reqestCharset;
}
public void setResponseCharset(String charset) {
responseCharset = charset;
}
public String getResponseCharset() {
return responseCharset == null ? "ISO-8859-1" : responseCharset;
}
public HttpResponse delete(String url) throws HttpException {
return delete(url, true);
}
public HttpResponse delete(String url, boolean isEncodeParameters)
throws HttpException {
return delete(url, null, isEncodeParameters);
}
public HttpResponse delete(String url, HttpPostParameter[] postParams,
boolean isEncodeParameters) throws HttpException {
return httpRequest(url, "DELETE", postParams, isEncodeParameters);
}
public HttpResponse post(String url) throws HttpException {
return post(url, true);
}
public HttpResponse post(String url, boolean isEncodeParameters)
throws HttpException {
return post(url, null, isEncodeParameters);
}
public HttpResponse post(String url, HttpPostParameter[] postParams,
boolean isEncodeParameters) throws HttpException {
return httpRequest(url, "POST", postParams, isEncodeParameters);
}
public HttpResponse get(String url) throws HttpException {
return get(url, true);
}
public HttpResponse get(String url, boolean isEncodeParameters)
throws HttpException {
return get(url, null, isEncodeParameters);
}
public HttpResponse get(String url, HttpPostParameter[] postParams,
boolean isEncodeParameters) throws HttpException {
return httpRequest(url, "GET", postParams, isEncodeParameters);
}
public HttpResponse httpRequest(String url, String httpMethod,
HttpPostParameter[] postParams, boolean isEncodeParameters)
throws HttpException {
int retriedCount;
int retry = retryCount;
HttpResponse res = null;
for (retriedCount = 0; retriedCount < retry; retriedCount++) {
int responseCode = -1;
log("发起请求:" + url);
try {
HttpURLConnection con = null;
OutputStream osw = null;
try {
con = getConnection(url);
setHeaders(con);
if ("POST".equals(httpMethod)) {
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);
String postParam = "";
if (postParams != null) {
postParam = encodeParameters(postParams,
isEncodeParameters);
}
byte[] bytes = postParam.getBytes(getRequestCharset());
log("请求长度: ", Integer.toString(bytes.length) + ",请求参数:"
+ postParam);
// con.setRequestProperty("Content-Length",
// Integer.toString(bytes.length));
osw = con.getOutputStream();
osw.write(bytes);
osw.flush();
osw.close();
} else if ("DELETE".equals(httpMethod)) {
con.setRequestMethod("DELETE");
} else {
con.setRequestMethod("GET");
}
res = new HttpResponse(con, getResponseCharset());
responseCode = con.getResponseCode();
if (DEBUG) {
log("Response: ");
Map<String, List<String>> responseHeaders = con
.getHeaderFields();
for (String key : responseHeaders.keySet()) {
List<String> values = responseHeaders.get(key);
for (String value : values) {
if (null != key) {
log(key + ": " + value);
} else {
log(value);
}
}
}
}
if (responseCode != OK) {
if (responseCode < INTERNAL_SERVER_ERROR
|| retriedCount >= retryCount) {
throw new HttpException(getCause(responseCode)
+ "\n" + res.asString(), responseCode);
}
} else {
break;
}
} finally {
try {
osw.close();
} catch (Exception ignore) {
}
}
} catch (IOException ioe) {
if (retriedCount >= retryCount) {
throw new HttpException(ioe.getMessage(), ioe, responseCode);
}
}
try {
if (DEBUG && null != res) {
res.asString();
}
log("Sleeping " + retryIntervalMillis
+ " millisecs for next retry.");
Thread.sleep(retryIntervalMillis);
} catch (InterruptedException ignore) {
}
}
return res;
}
public String encodeParameters(HttpPostParameter[] postParams,
boolean isEncodeParameters) {
StringBuffer buf = new StringBuffer();
for (int j = 0; j < postParams.length; j++) {
if (j != 0) {
buf.append("&");
}
if (isEncodeParameters) {
buf.append(encode(postParams[j].name)).append("=")
.append(encode(postParams[j].value));
} else {
buf.append(postParams[j].name).append("=")
.append(postParams[j].value);
}
}
return buf.toString();
}
/**
* rfc3986
*
* @param value
* @return
*/
public String encode(String value) {
String encoded = null;
try {
encoded = URLEncoder.encode(value, getRequestCharset());
} catch (UnsupportedEncodingException ignore) {
}
StringBuffer buf = new StringBuffer(encoded.length());
char focus;
for (int i = 0; i < encoded.length(); i++) {
focus = encoded.charAt(i);
if (focus == '*') {
buf.append("%2A");
} else if (focus == '+') {
buf.append("%20");
} else if (focus == '%' && (i + 1) < encoded.length()
&& encoded.charAt(i + 1) == '7'
&& encoded.charAt(i + 2) == 'E') {
buf.append('~');
i += 2;
} else {
buf.append(focus);
}
}
return buf.toString();
}
private void setHeaders(HttpURLConnection connection) {
for (String key : requestHeaders.keySet()) {
connection.addRequestProperty(key, requestHeaders.get(key));
log(key + ": " + requestHeaders.get(key));
}
}
public void setRequestHeader(String name, String value) {
requestHeaders.put(name, value);
}
public String getRequestHeader(String name) {
return requestHeaders.get(name);
}
private HttpURLConnection getConnection(String url)
throws MalformedURLException, IOException {
HttpURLConnection con = (HttpURLConnection) new URL(url)
.openConnection();
if (connectionTimeout > 0) {
con.setConnectTimeout(connectionTimeout);
}
if (readTimeout > 0) {
con.setReadTimeout(readTimeout);
}
return con;
}
private static void log(String message) {
if (DEBUG)
没有合适的资源?快使用搜索试试~ 我知道了~
Android ndroid 查询工具源代码.zip
共122个文件
class:50个
java:25个
png:22个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 12 浏览量
2022-06-14
15:08:12
上传
评论
收藏 338KB ZIP 举报
温馨提示
Android ndroid 查询工具源代码.zip
资源详情
资源评论
资源推荐
收起资源包目录
Android ndroid 查询工具源代码.zip (122个子文件)
resources.ap_ 87KB
SmallTools.apk 113KB
proguard.cfg 1KB
HttpClientHelper.class 10KB
HttpResponse.class 7KB
HttpPostParameter.class 6KB
SmallToolsActivity.class 5KB
ApiImpl.class 4KB
QueryAddress.class 3KB
QueryIDCard.class 3KB
QueryPhone.class 3KB
UpdateApp.class 3KB
UpdateApp$1.class 3KB
BaseActivity.class 2KB
ItemAdapter.class 2KB
BaseLayout.class 2KB
QueryAddress$2.class 2KB
QueryIDCard$2.class 2KB
QueryPhone$2.class 2KB
QueryAddress$1.class 2KB
QueryIDCard$1.class 2KB
QueryPhone$1.class 2KB
DeviceHelper.class 2KB
SmallToolsActivity$1.class 2KB
R$string.class 1KB
SmallToolsActivity$2.class 1KB
AppInfoHelper.class 1KB
UpdateApp$1$1.class 1KB
AddressInfo.class 1KB
HttpException.class 1KB
NumberItem.class 1KB
QueryAddress$3.class 1KB
QueryIDCard$3.class 1KB
QueryPhone$3.class 1KB
AppUpdateInfo.class 1KB
IDCardInfo.class 1KB
HttpResponse$1.class 1KB
UpdateApp$2.class 1KB
InternetHelper.class 1KB
R$drawable.class 1015B
Splash$1.class 927B
Constants.class 852B
R$id.class 845B
R.class 787B
PhoneInfo.class 772B
Splash.class 753B
R$layout.class 648B
R$style.class 595B
R$dimen.class 459B
R$anim.class 443B
R$color.class 420B
DataListHolder.class 411B
R$attr.class 358B
.classpath 280B
classes.dex 49KB
HttpClientHelper.java 9KB
HttpPostParameter.java 5KB
R.java 5KB
HttpResponse.java 5KB
SmallToolsActivity.java 4KB
QueryAddress.java 4KB
QueryIDCard.java 4KB
QueryPhone.java 4KB
ApiImpl.java 3KB
UpdateApp.java 3KB
BaseActivity.java 2KB
ItemAdapter.java 2KB
BaseLayout.java 1KB
DeviceHelper.java 1KB
Constants.java 985B
AppInfoHelper.java 928B
AddressInfo.java 824B
InternetHelper.java 797B
HttpException.java 763B
AppUpdateInfo.java 737B
NumberItem.java 694B
IDCardInfo.java 676B
Splash.java 639B
PhoneInfo.java 395B
DataListHolder.java 196B
icon.png 16KB
icon.png 16KB
icon.png 16KB
menu_update.png 3KB
menu_help.png 2KB
title_back_selected.png 1KB
title_back_normal.png 1KB
title_home_selected.png 1KB
title_home_normal.png 1KB
number_query.png 1KB
number_finance.png 1019B
title_back_selected.png 990B
title_back_normal.png 956B
title_home_selected.png 854B
number_airline.png 835B
title_home_normal.png 830B
number_operator.png 809B
number_life.png 789B
number_ticket.png 418B
titlebar_bg_nor.png 289B
共 122 条
- 1
- 2
BryanDing
- 粉丝: 297
- 资源: 5587
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0