HttpClient get, post使用

来源:互联网 发布:数据库分为三种类型 编辑:程序博客网 时间:2024/06/10 08:34
public class HttpClientAdapter {
public static BasicHeader[] headers = new BasicHeader[10];

static {

//封装头信息

headers[0] = new BasicHeader("Appkey", "12343");
headers[1] = new BasicHeader("Udid", "");// 手机串号
headers[2] = new BasicHeader("Os", "android");//
headers[3] = new BasicHeader("Osversion", "");//
headers[4] = new BasicHeader("Appversion", "");// 1.0
headers[5] = new BasicHeader("Sourceid", "");//
headers[6] = new BasicHeader("Ver", "");
headers[7] = new BasicHeader("Userid", "");
headers[8] = new BasicHeader("Usersession", "");
headers[9] = new BasicHeader("Unique", "");
}


private HttpClient client;


private HttpRequest request;


private HttpPost post;
private HttpGet get;


private HttpResponse response;


public HttpClientAdapter() {
// 创建client


HttpParams parms = new BasicHttpParams();
parms.setParameter("charset", HTTP.UTF_8);
HttpConnectionParams.setConnectionTimeout(parms, 8 * 1000);

HttpConnectionParams.setSoTimeout(parms, 8 * 1000);

//设置一些超时连接和编码格式等连接方式参数

client = new DefaultHttpClient(parms);
/*
* if (StringUtils.isNotBlank(GlobalParams.PROXY_IP)) { // WAP // 代理信息处理
* HttpHost value = new HttpHost(GlobalParams.PROXY_IP,
* GlobalParams.PROXY_PORT);
* client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
* value); }
*/
}


/**
* 发送头像文件到服务器端

* @param uri
*            目标地址
* @param xml
*/
public String sendPostRequest(String uri, List<NameValuePair> nameValuePairs) {
post = new HttpPost(uri);


post.addHeader("charset", HTTP.UTF_8);


HttpContext localContext = new BasicHttpContext();
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
//带有图片等上传资源的处理方式,该方法使用需要引入apache http 的资源包(最好用apache提供的全部jar包,而不是用android自带的,否则会出现异常)

for (int index = 0; index < nameValuePairs.size(); index++) {
if (nameValuePairs.get(index).getName().equalsIgnoreCase("pic")) {
// If the key equals to "pic", we use FileBody to transfer
// the data
entity.addPart(nameValuePairs.get(index).getName(),
new FileBody(new File(nameValuePairs.get(index)
.getValue())));
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(),
new StringBody(

nameValuePairs.get(index).getValue(),

//在此处使用编码格式,防止乱编码,其他地方设置是无效的

Charset.forName("UTF-8")));
// new Stringbo
}
}


post.setEntity(entity);

response = client.execute(post, localContext);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(),
ConstantValue.CHARSET);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}


public String sendGetRequest(String uri) {
get = new HttpGet(uri);
get.setHeaders(headers);
try {
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(),
ConstantValue.CHARSET);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**

带参数的get请求可以用?的拼接方式,也可以用List<NameValuePair>的方式实现

**/

public String sendGetRequest(String host, int port, String path,
List<NameValuePair> params) {
URI uri = null;
try {
uri = URIUtils.createURI("http", host, port, path,
URLEncodedUtils.format(params, "UTF-8"), null);
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
get = new HttpGet(uri);
get.setHeaders(headers);
try {
response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(),
ConstantValue.CHARSET);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

}

ListParams 参数构建

List<NameValuePair> storeList=new ArrayList<NameValuePair>();
NameValuePair storeIdvp=new BasicNameValuePair("storeId", storeId);
storeList.add(storeIdvp);
NameValuePair qNvp=new BasicNameValuePair("number",queueNumber+"");
storeList.add(qNvp);

原创粉丝点击