博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android之HttpClient
阅读量:5827 次
发布时间:2019-06-18

本文共 4060 字,大约阅读时间需要 13 分钟。

import java.io.IOException;

import java.io.InputStream;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.util.Log;

public class CustomHttpClient {

private HttpClient httpClient;

private String className = getClass().getName().toString();

private String TAG = className;

public CustomHttpClient() {

this.httpClient = getHttpClient(10000, 10000);
}

public CustomHttpClient(int conTimeout, int soTimeout) {

this.httpClient = getHttpClient(conTimeout, soTimeout);
}

public CustomHttpClient(HttpClient httpClient) {

this.httpClient = httpClient;
}

static public HttpClient getHttpClient(int conTimeout, int soTimeout) {

HttpParams httpParams;
httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, conTimeout);
HttpConnectionParams.setSoTimeout(httpParams, soTimeout);
HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
HttpClientParams.setRedirecting(httpParams, true);
String userAgent = "Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83)";
HttpProtocolParams.setUserAgent(httpParams, userAgent);
return new DefaultHttpClient(httpParams);
}

@SuppressWarnings("rawtypes")

public String Getx(String prefixurl, Map<Object, String> params) {
String paramstr = "";
String url = prefixurl;
Iterator<?> iter = params.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
paramstr += "&" + key + "=" + val;
}
if (!paramstr.equals("")) {
paramstr = paramstr.replaceFirst("&", "?");
url += paramstr;
}
return Get(url);
}

public String Get(String url) {

HttpGet httpRequest = new HttpGet(url);
String strResult = null;
try {
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(httpResponse.getEntity());
} else {
Log.v(TAG, "Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return strResult;
}

public String Post(String url, List<NameValuePair> params) {

HttpPost httpRequest = new HttpPost(url);
String strResult = null;
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
strResult = EntityUtils.toString(httpResponse.getEntity());
} else {
Log.v(TAG, "Error Response: "
+ httpResponse.getStatusLine().toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return strResult;
}

public InputStream Download(String url) {

InputStream in = null;
HttpGet httpget = new HttpGet(url);
try {
HttpResponse resp = httpClient.execute(httpget);
if (HttpStatus.SC_OK == resp.getStatusLine().getStatusCode()) {
HttpEntity entity = resp.getEntity();
in = entity.getContent();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return in;
}
}

转载于:https://www.cnblogs.com/colife/p/3483699.html

你可能感兴趣的文章
docker 下 安装rancher 笔记
查看>>
spring两大核心对象IOC和AOP(新手理解)
查看>>
数据分析相关
查看>>
Python LDAP中的时间戳转换为Linux下时间
查看>>
微信小程序蓝牙连接小票打印机
查看>>
环境错误2
查看>>
C++_了解虚函数的概念
查看>>
全新jmeter视频已经上架
查看>>
Windows 8下如何删除无线配置文件
查看>>
oracle系列(五)高级DBA必知的Oracle的备份与恢复(全录收集)
查看>>
hp 服务器通过串口重定向功能的使用
查看>>
国外10大IT网站和博客网站
查看>>
android第十一期 - SmoothSwitchLibrary仿IOS切换Activity动画效果
查看>>
zabbix 批量web url监控
查看>>
MongoDB CookBook读书笔记之导入导出
查看>>
shell如何快速锁定所有账号
查看>>
HTML 5实现的手机摇一摇
查看>>
Linux 文件IO理解
查看>>
Ninject 2.x细说---2.绑定和作用域
查看>>
30个非常时尚的网页联系表单设计优秀示例
查看>>