在Android中,除了使用java.net包下的API访问HTTP服务之外,Android SDK附带了Apache的HttpClient API。Apache HttpClient是一个完善的HTTP客户端,它提供了对HTTP协议的全面支持,可以使用HTTP GET和POST进行访问。HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
HttpGet
<span style="white-space:pre"> </span>HttpClient client = new DefaultHttpClient();
<span style="white-space:pre"> </span>HttpGet get = new HttpGet(PATH + "/servlet/HttpRequest?id=1001&name=john&age=60");
<span style="white-space:pre"> </span>HttpResponse response = client.execute(get);
<span style="white-space:pre"> </span>if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
<span style="white-space:pre"> </span>String result = EntityUtils.toString(response.getEntity());
<span style="white-space:pre"> </span>}
HttpPost
使用了HttpPost,URL后面并没有附带参数信息,参数信息被包装成一个由NameValuePair类型组成的集合的形式,然后经过UrlEncodedFormEntity处理后调用HttpPost的setEntity方法进行参数设置,最后由HttpClient执行。
<span style="white-space:pre"> </span>HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(PATH+"/servlet/HttpRequest");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "1001"));
params.add(new BasicNameValuePair("name", "john"));
params.add(new BasicNameValuePair("age", "60"));
HttpEntity formEntity = new UrlEncodedFormEntity(params);
post.setEntity(formEntity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream is = response.getEntity().getContent();
String result = inStream2String(is);
System.out.println("result:" + result);
}
HttpPost上传文件
HttpClient client = new DefaultHttpClient();
// 视频路径 /mnt/sdcard/DCIM/100MEDIA/VIDEO0007.mp4
HttpPost post = new HttpPost(PATH+"/servlet/HttpUpload");
MultipartEntity multipartEntity = new MultipartEntity();
FileBody filebodyVideo = new FileBody(new File("mnt/sdcard/DCIM/100MEDIA/VIDEO0007.mp4"));
multipartEntity.addPart("file", filebodyVideo);
multipartEntity.addPart("desc", new StringBody(
"this is description."));
post.setEntity(multipartEntity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream is = response.getEntity().getContent();
String result = inStream2String(is);
}
服务器端代码:
public class HttpUpload extends HttpServlet{
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> list = upload.parseRequest(request);
Iterator<FileItem> item = list.iterator();
while (item.hasNext()) {
FileItem fileItem = item.next();
if (fileItem.isFormField()) {
String paramName = fileItem.getName();
String paramValue = fileItem.getString();
System.out.println("paramName:" + paramName
+ "-------paramValue:" + paramValue);
} else {
String fileName = fileItem.getName();
byte[] data = fileItem.get();
String filePath = getServletContext().getRealPath(
"/files")
+ "/" + fileName;
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(data);
fos.flush();
fos.close();
response.getWriter().write("SUCESS");
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
单例HttpClient
1.扩展系统默认的Application,并应用在项目中。
2.使用HttpClient类库提供的ThreadSafeClientManager来创建和管理HttpClient。
重写了onCreate()方法,在系统启动时就创建一个HttpClient;重写了onLowMemory()和onTerminate()方法,在内存不足和应用结束时关闭连接,释放资源。需要注意的是,当实例化DefaultHttpClient时,传入一个由ThreadSafeClientConnManager创建的一个ClientConnectionManager实例,负责管理HttpClient的HTTP连接。
public class MyApplication extends Application{
private HttpClient client;
@Override
public void onCreate() {
super.onCreate();
client = this.createHttpClient();
}
/**
* 获取HttpClient实例
* @return
*/
public HttpClient getHttpClient(){
return client;
}
/**
* 创建HttpClient实例
* @return
*/
private HttpClient createHttpClient(){
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80));
schReg.register(new Scheme("https",SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager conn = new ThreadSafeClientConnManager(params, schReg);
HttpClient client = new DefaultHttpClient(conn, params);
return client;
}
/**
* 关闭连接管理器并且释放资源
*/
private void shutDownHttpClient(){
if(client != null && client.getConnectionManager() != null){
client.getConnectionManager().shutdown();
}
}
/**
* 在内存不足关闭连接
*/
@Override
public void onLowMemory() {
super.onLowMemory();
shutDownHttpClient();
}
/**
* 应用结束时关闭连接
*/
@Override
public void onTerminate() {
super.onTerminate();
shutDownHttpClient();
}
}