1 /**
2 * 测试HttpClient2种请求网络方式的Activity
3 * get和post
4 *
5 */
6 public class HttpClientActivity extends Activity {
7 private HttpParams httpParams ;
8 private HttpClient httpClient ;
9 @Override
10 protected void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.activity_httpclient);
13 EditText et1 = (EditText) this.findViewById(R.id.et1);
14 List<NameValuePair> params = new ArrayList<NameValuePair>();
15 params.add(new BasicNameValuePair("email", "[email protected]"));
16 params.add(new BasicNameValuePair("password", "954619"));
17 params.add(new BasicNameValuePair("remember", "1"));
18 params.add(new BasicNameValuePair("from", "kx"));
19 params.add(new BasicNameValuePair("login", "登录"));
20 params.add(new BasicNameValuePair("refcode", ""));
21 params.add(new BasicNameValuePair("refuid", "0"));
22 Map params2 = new HashMap();
23 params2.put("hl", "zh-CN");
24 params2.put("source", "hp");
25 params2.put("q", "haha");
26 params2.put("aq", "f");
27 params2.put("aqi", "g10");
28 params2.put("aql", "");
29 params2.put("oq", "");
30 String url2 = "http://www.google.cn/search";
31 String url = "http://wap.kaixin001.com/home/";
32
33 getHttpClient();
34 et1.setText(doPost(url, params));
35 // et1.setText(doGet(url2, params2));
36
37 }
38 /**
39 * get 方式发送网络请求
40 * @param url url地址
41 * @param params 请求参数
42 * @return 返回请求结果statusLine状态行数据
43 */
44 public String doGet(String url,Map params){
45 /* 建立HTTPGet对象 */
46 String paramStr = "";
47 Iterator iter = params.entrySet().iterator();
48 while(iter.hasNext()){
49 Map.Entry entry = (Entry) iter.next();
50 Object key = entry.getKey();
51 Object val = entry.getValue();
52 paramStr += paramStr = "&" + key + "=" + val ;
53 }
54 if(!paramStr.equals("")){
55 paramStr.replaceFirst("&", "?");
56 url += paramStr;
57 }
58 HttpGet httpRequest = new HttpGet(url);
59 String strResult = "doGetError";
60 try {
61 /* 发动请求并等待回应 */
62 HttpResponse httpResponse = httpClient.execute(httpRequest);
63 /* 若状态码为200 ok */
64 if(httpResponse.getStatusLine().getStatusCode() == 200){
65 /* 读返回数据 */
66 strResult = EntityUtils.toString(httpResponse.getEntity());
67 }else{
68 strResult = "Error Response : " + httpResponse.getStatusLine().toString();
69 }
70 } catch (ClientProtocolException e) {
71 strResult = e.getMessage().toString();
72 e.printStackTrace();
73 } catch (IOException e) {
74 strResult = e.getMessage().toString();
75 e.printStackTrace();
76 }
77 return strResult;
78 }
79 /**
80 *
81 * @param url
82 * @param params
83 * @return 服务器返回的数据 httpResponse.getEntity()
84 */
85 public String doPost(String url,List<NameValuePair> params){
86 /* 建立HTTPPost对象 */
87 HttpPost httpRequest = new HttpPost(url);
88 String strResult = "doPostError";
89 try {
90 /* 添加请求参数到请求对象 */
91 httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
92 /* 发送请求并等待响应 */
93 HttpResponse httpResponse = httpClient.execute(httpRequest);
94 /* 若状态码 为 200 */
95 if(httpResponse.getStatusLine().getStatusCode() == 200){
96 /* 读返回数据 */
97 strResult = EntityUtils.toString(httpResponse.getEntity());
98 }else{
99 strResult = "Error Response : " + httpResponse.getStatusLine().toString();
100 }
101 } catch (ClientProtocolException e) {
102 strResult = e.getMessage().toString();
103 e.printStackTrace();
104 } catch (IOException e) {
105 strResult = e.getMessage().toString();
106 e.printStackTrace();
107 }
108 return strResult;
109 }
110 /**
111 * 获取一个HttpClient对象
112 * @return 返回一个HttpClient对象
113 */
114 public HttpClient getHttpClient(){
115 //创建HttpParams以用来设置HTTP参数(这一部分不是必须的)
116 this.httpParams = new BasicHttpParams();
117 //设置连接超时和socket超时,以及socket缓存大小
118 HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
119 HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
120 HttpConnectionParams.setSocketBufferSize(httpParams, 8196);
121 //设置重定向,默认为true
122 HttpClientParams.setRedirecting(httpParams, true);
123 //设置user agent
124 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
125 HttpProtocolParams.setUserAgent(httpParams, userAgent);
126 //创建一个HttpClient实例
127 //注意 HttpClient httpClient = new HttpClient();是Commons HttpClient 中的用法
128 //在Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient
129 httpClient = new DefaultHttpClient();
130 return httpClient;
131 }
132 }
HttpClient的一种简单实现Demo,布布扣,bubuko.com
时间: 2024-10-14 20:27:20