httpUtils封装
来源:互联网 发布:游戏王vrains知乎 编辑:程序博客网 时间:2024/05/29 15:10
public static HttpEntity getHttpEntity(int request,
List<NameValuePair> nameValuePairLists, String url)
throws Exception {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
HttpUriRequest httpUriRequest = null;
HttpResponse httpResponse = null;
HttpEntity httpEntity = null;
switch (request) {
case 0:
StringBuffer buffer = new StringBuffer(url);
if (nameValuePairLists != null && nameValuePairLists.size() > 0) {
buffer.append("?");
for (NameValuePair nameValuePair : nameValuePairLists) {
buffer.append(nameValuePair.getName());
buffer.append("=");
buffer.append(nameValuePair.getValue());
buffer.append("&");
}
buffer.deleteCharAt(buffer.lastIndexOf("&"));
httpUriRequest = new HttpGet(buffer.toString());
}
break;
case 1:
httpUriRequest = new HttpPost(url);
if (nameValuePairLists != null && nameValuePairLists.size() > 0) {
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(
nameValuePairLists, "utf-8");
((HttpPost) httpUriRequest).setEntity(encodedFormEntity);
}
break;
}
httpResponse = httpClient.execute(httpUriRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
httpEntity = httpResponse.getEntity();
}
return httpEntity;
}
List<NameValuePair> nameValuePairLists, String url)
throws Exception {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
HttpUriRequest httpUriRequest = null;
HttpResponse httpResponse = null;
HttpEntity httpEntity = null;
switch (request) {
case 0:
StringBuffer buffer = new StringBuffer(url);
if (nameValuePairLists != null && nameValuePairLists.size() > 0) {
buffer.append("?");
for (NameValuePair nameValuePair : nameValuePairLists) {
buffer.append(nameValuePair.getName());
buffer.append("=");
buffer.append(nameValuePair.getValue());
buffer.append("&");
}
buffer.deleteCharAt(buffer.lastIndexOf("&"));
httpUriRequest = new HttpGet(buffer.toString());
}
break;
case 1:
httpUriRequest = new HttpPost(url);
if (nameValuePairLists != null && nameValuePairLists.size() > 0) {
UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(
nameValuePairLists, "utf-8");
((HttpPost) httpUriRequest).setEntity(encodedFormEntity);
}
break;
}
httpResponse = httpClient.execute(httpUriRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
httpEntity = httpResponse.getEntity();
}
return httpEntity;
}
0 0
- httpUtils封装
- 安卓笔记-封装的HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- Httputils
- httpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- 自己封装的联网工具类HttpUtils的使用
- 控制台绘制正弦曲线和余弦曲线同时显示
- 同余与模运算
- HDU 1533 Going Home(最小费用最大流模版)
- svn提交.a文件
- 全球通史(2)
- httpUtils封装
- 提交.a文件有三个方法
- 判断一个单链表是否有环及环的链接点(转)
- 股票 W&R威廉指标形态图解
- 【bzoj4027】【HEOI2015】兔子与樱花【贪心】
- 在浏览器上实现自动引导(五)
- 在一个makefile中输出一个程序的debug版本和release版本
- 设计模式学习笔记-观察者模式
- g++ 有没有像VC那样的release/debug