Apache提供HttpClient,它对java.net中的类做了封装和抽象,更适合在Android上开发应用。
HttpClient应用开发几个类:
1. ClientConnectionManager是客户端连接管理器的接口,
提供以下几个抽象方法:
closeIdleConnections, 关闭空闲的连接
releaseConnection, 释放一个连接
requestConnection, 请求一个新的连接
shutdown 关闭ClientConnectionManager并释放资源
2. DefaultHttpClient是一个默认的Http客户端,可以用来创建一个http连接。
HttpClient httpClient = new DefaultHttpClient();
3. HttpResponse是一个http连接响应
HttpResponse response = httpClient.execute(httpRequest);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
- package com.lingdududu.http;
- import java.io.InputStream;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class HttpGetActivity extends Activity {
- String uri = "http://developer.android.com/";
- final String TAG_STRING = "TAG";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- try {
- //得到HttpClient对象
- HttpClient getClient = new DefaultHttpClient();
- //得到HttpGet对象
- HttpGet request = new HttpGet(uri);
- //客户端使用GET方式执行请教,获得服务器端的回应response
- HttpResponse response = getClient.execute(request);
- //判断请求是否成功
- if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
- Log.i(TAG_STRING, "请求服务器端成功");
- //获得输入流
- InputStream inStrem = response.getEntity().getContent();
- int result = inStrem.read();
- while (result != -1){
- System.out.print((char)result);
- result = inStrem.read();
- }
- //关闭输入流
- inStrem.close();
- }else {
- Log.i(TAG_STRING, "请求服务器端失败");
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- package com.androidbook.services.httppost;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import android.app.Activity;
- import android.os.Bundle;
- public class HttpPostActivity extends Activity {
- String uri = "http://developer.android.com/";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- BufferedReader in = null;
- try {
- HttpClient client = new DefaultHttpClient();
- HttpPost request = new HttpPost("http://code.google.com/android/");
- //使用NameValuePair来保存要传递的Post参数
- List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- //添加要传递的参数
- postParameters.add(new BasicNameValuePair("id", "12345"));
- postParameters.add(new BasicNameValuePair("username", "dave"));
- //实例化UrlEncodedFormEntity对象
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
- postParameters);
- //使用HttpPost对象来设置UrlEncodedFormEntity的Entity
- request.setEntity(formEntity);
- HttpResponse response = client.execute(request);
- in = new BufferedReader(
- new InputStreamReader(
- response.getEntity().getContent()));
- StringBuffer string = new StringBuffer("");
- String lineStr = "";
- while ((lineStr = in.readLine()) != null) {
- string.append(lineStr + "\n");
- }
- in.close();
- String resultStr = string.toString();
- System.out.println(resultStr);
- } catch(Exception e) {
- // Do something about exceptions
- } finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }