文章目录
  1. 1. 简单介绍
  2. 2. 实例

简单介绍

本篇介绍使用Android中自带的Apache的HttpClient接口进行HTTP操作,为了方便,这里还是使用Thread来简单操作。

实例

以下是通过HttpClient的Get方法和Post方法获取湖南文理学院班级课表的有关信息的源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.carlos.httpclient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

thread.start();
}

Thread thread=new Thread(){
public void run() {
get();
post();
};
};

void get(){
String url="http://www.huas.cn:83/jwweb/ZNPK/KBFB_ClassSel.aspx";
//HttpGet连接对象
HttpGet get=new HttpGet(url);
//取得HttpClient对象
HttpClient client=new DefaultHttpClient();
//设置连接超时
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10*000);
//设置读取时间超时
HttpConnectionParams.setSoTimeout(client.getParams(), 10*000);
//设置从连接池中取连接超时
ConnManagerParams.setTimeout(client.getParams(), 10*000);
//请求HttpClient,取得HttpResponse
try {
HttpResponse response=client.execute(get);
if (response.getStatusLine().getStatusCode()==200) {
//取得返回的字符串
String result=EntityUtils.toString(response.getEntity());
Log.i("Http通信之HttpClient", "get方法"+result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

void post(){
String url="http://www.huas.cn:83/jwweb/ZNPK/KBFB_ClassSel_rpt.aspx";
//取得HttpClient对象
HttpClient client=new DefaultHttpClient();
//HttpPost连接对象
HttpPost post=new HttpPost(url);
//使用BasicNameValuePair来保存要传递的Post参数
List<BasicNameValuePair> pairs=new ArrayList<BasicNameValuePair>();
pairs.add(new BasicNameValuePair("chkrxkc", "1"));
pairs.add(new BasicNameValuePair("Sel_XNXQ", "20141"));
pairs.add(new BasicNameValuePair("Sel_XZBJ", "2013120402"));
pairs.add(new BasicNameValuePair("Submit01", "????"));
pairs.add(new BasicNameValuePair("txtxzbj", ""));
pairs.add(new BasicNameValuePair("type", "2"));
//设置连接超时
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10*000);

try {
//设置字符集
HttpEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8);
post.setEntity(entity);
HttpResponse response=client.execute(post);
if (response.getStatusLine().getStatusCode()==HttpStatus.SC_OK) {
String result=EntityUtils.toString(response.getEntity());
Log.i("Http通信之HttpClient", "post方法"+result);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Logcat日志中打印出的结果

文章目录
  1. 1. 简单介绍
  2. 2. 实例