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

简单介绍

在Android中,通常需要网络通信,HTTP协议通信比较常见,经常通过HttpURLConnection和HttpClient接口来实现,本篇先介绍在Android中使用HttpURLConnection来进行网络通信。

实例

进行网络多线程操作使用AsyncTask异步类更好,但这里主要介绍HttpURlConnection的使用就通过Thread线程来简单操作,不要忘了加允许联网的权限。还是以湖南文理学院班级课表为例,以下是简单实现的源代码

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
106
107
108
109
110
111
112
113
package com.carlos.httpurlconnection;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

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();
}

// 创建线程,里面调用下面写的get和post方法
Thread thread = new Thread() {
public void run() {
get();
post();
};
};

// HttpURLConnection之get方法
void get() {
URL url;
try {
url = new URL("http://www.huas.cn:83/jwweb/ZNPK/KBFB_ClassSel.aspx");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(10 * 000);// 设置连接超时时间
connection.connect();
// 通过InputStreamReader读取流,设置编码防止中文乱码
InputStreamReader isr = new InputStreamReader(
connection.getInputStream(), "GBK");
BufferedReader reader = new BufferedReader(isr);
String inputLine = null;
String resultData = "";
while ((inputLine = reader.readLine()) != null) {
resultData += inputLine;
}
Log.i("Http通信之HttpURLConnection", "get方法\n" + resultData);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

// HttpURLConnection之post方法
void post() {
URL url;
try {
url = new URL("http://www.huas.cn:83/jwweb/ZNPK/KBFB_ClassSel_rpt.aspx");
// 使用HttpURLConnection打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
// 设置请求方法为POST
connection.setRequestMethod("POST");
// 请求不能使用缓存
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
// 配置请求的Content-type
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
// 连接
connection.connect();
DataOutputStream dos = new DataOutputStream(
connection.getOutputStream());

// 需要post的参数
StringBuffer buffer = new StringBuffer();
buffer.append("chkrxkc=1").append("&").append("Sel_XNXQ=20141")
.append("&").append("Sel_XZBJ=2013120402").append("&")
.append("Submit01=????").append("&").append("txtxzbj=")
.append("&").append("type=2");

byte[] bytes = buffer.toString().getBytes();
dos.write(bytes);
dos.flush();
dos.close();

InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "GBK");
BufferedReader reader = new BufferedReader(isr);
String temp;
StringBuffer sb = new StringBuffer();
while ((temp = reader.readLine()) != null) {
sb.append(temp);
}
is.close();
connection.disconnect();
String result = sb.toString();
Log.i("Http通信之HttpURLConnection", "post方法\n" + result);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Logcat日志中打印出的结果

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