在android系统中提供了三种通信接口
标准的java接口 java.net
apache接口 org.apache.http
android网络接口 android.net.http
apache接口中主要类HttpClient,HttpResponse,HttpEntity,HttpGet,HttpPost。使用HttpGet和HttpPost封装Get和Post请求,然后使用HttpClient类的execute方法发送Post和Get请求,并返回服务器响应数据HttpResponse。
还可以设置连接和读取起始时间并新建HttpClient。
HttpParams httpParams = new DefaultedHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,3000);
HttpConnectionParams.setSoTimeout(httpParams,5000);
HttpClient httpClient = new DefaultHttpClient(httpParams);
将服务器返回的结果在HttpResponse中,然后转换成字符串。
方法1:
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = reader.readLine()) != null){
//对line进行处理
}
方法2:
String content = inStream2String(response.getEntity().getContent());
/**
* 将输入流转换成字符串
* @param is
* @return
* @throws Exception
*/
private String inStream2String(InputStream is) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = -1;
while ((len = is.read(buf)) != -1) {
baos.write(buf, 0, len);
}
return new String(baos.toByteArray());
}
方法3:
if(response.getStatusLine().getStatusCode == 200){
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
if(bytes != null){
String strXml = new String(bytes,"utf-8");
}
}
HttpClient就像一个浏览器,对于整个应用,只需要一个HttpClient就够了,因此可以得出:使用单例模式就可以实现,例如下面的代码:
public class CustomHttpClient {
private HttpClient customHttpClient;
public static HttpClient getCustomHttpClient(){
if(null == customHttpClient){
customHttpClient = new DefaultHttpClient();
}
return customHttpClient;
}
}
如果同时有多个请求需要处理昵?使用多线程。下面的代码很好地解决了多线程问题。通过getHttpClient()方法为HttpClient配置了一些基本参数和超时设置,然后使用ThreadSafeClientConnManager来创建线程安全的HttpClient。
public class CustomHttpClient {
private static final String CHARSET = "HTTP.UTF_8";
private HttpClient customHttpClient;
private CustomHttpClient(){
}
public static synchronized HttpClient getHttpClient(){
if(null == customHttpClient){
HttpParams params = new BasicHttpParams();
//设置一些基本参数
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentChar(params, CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Mozilla/5.0(Linux;U;Android 3.0;en-us;Nexus One Build.FRG83)"
+"AppkeWebKit/553.1(KHTML,Like Gecko) Version/4.0 Mobile Safari/533.1");
//超时设置
//从连接池中取连接的超时时间
ConnManagerParams.setTimeOut(params, 1000);
/** 连接超时 **/
HttpConnectionParams.setConnectionTimeOut(params, 2000);
// 请求超时
HttpConnectionParams.setSoTimeout(params, 4000);
/* 设置我们的HttpClient支持HTTP和HTTPS两种模式*/
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory,getSocketFactory(), 443));
//使用线程安全的连接管理来创建HttpClient
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
customHttpClient = new DefaultHttpClient(conMgr, params);
}
return customHttpClient;
}
}