java发起http请求

demo


0. HttpUtil 类

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
package com.kompasim.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpUtil {
String path = null;
URL url = null;
HttpURLConnection urlConnection = null;
private String resultData = "";
public HttpUtil(String p){
path = p;
}
public void open(String string){
try {
url = new URL(path + "?" + string);
System.out.println("url is : " + url);
urlConnection = (HttpURLConnection)url.openConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setHeader(String name, String value){
urlConnection.addRequestProperty(name,value);
}
public String get(){
try {
//headers
urlConnection.setRequestMethod("GET");
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout (5000);
urlConnection.connect();
if (urlConnection.getResponseCode()!=200){
System.out.println("err : get网络请求失败,返回200");
return "error";
}else {
InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line=br.readLine())!=null){
resultData += line;
}
isr.close();
br.close();
urlConnection.disconnect();
return resultData.toString();
}
}catch (Exception e){
e.printStackTrace();
System.out.println("err : " + e.getStackTrace().toString());
return "error";
}
}
public String post(String encodedString){
try {
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(5*1000);
}catch (Exception e){
System.out.println("err : " + e.getStackTrace().toString());
}
try{
urlConnection.setRequestMethod("POST");
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
urlConnection.connect();
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
// 参数
// 将要上传的内容写入流中
out.writeBytes(encodedString);
// 刷新关闭
out.flush();
out.close();
if (urlConnection.getResponseCode()!=200){
System.out.println("err : post网络请求失败,返回200");
return "error";
}else {
InputStreamReader isr = new InputStreamReader(urlConnection.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line=br.readLine())!=null){
resultData += line;
}
isr.close();
urlConnection.disconnect();
return resultData.toString();
}
}catch(Exception e){
System.out.println("err : " + e.getStackTrace().toString());
return "error";
}
}
}

1. 发起get请求

1
2
3
4
5
6
HttpUtil httpUtil = new HttpUtil("http://api.kompasim.cn/humor.php");
String queryStr = "bodyName1=" + URLEncoder.encode("bodyValue1","UTF-8") + "&bodyName2=" + URLEncoder.encode("bodyValue2","UTF-8");
httpUtil.open(queryStr);
httpUtil.setHeader("headerNmae1", "headerValue1");
httpUtil.setHeader("headerNmae2", "headerValue2");
String resultStr = httpUtil.get();

2. 发起post请求

1
2
3
4
5
6
HttpUtil httpUtil = new HttpUtil("http://api.kompasim.cn/humor.php");
httpUtil.open("");
httpUtil.setHeader("headerNmae1", "headerValue1");
httpUtil.setHeader("headerNmae2", "headerValue2");
String queryStr = "bodyName1=" + URLEncoder.encode("bodyValue1","UTF-8") + "&bodyName2=" + URLEncoder.encode("bodyValue2","UTF-8");
String resultStr = httpUtil.post(queryStr);

3. 线程间通信

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
// handler
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
String resultStr = (String)msg.obj;
// do something
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(resultStr);
break;
default:
break;
}
}
};
// timer task
TimerTask tt = new TimerTask() {
@Override
public void run() {
// do post or do get
String resultStr = "";
Message msg = new Message();
msg.what = 5;
msg.obj = resultStr;
handler.sendMessage(msg);
}
};
Timer t = new Timer(true);
t.schedule(tt, 0, 1000);

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 0. HttpUtil 类
  2. 2. 1. 发起get请求
  3. 3. 2. 发起post请求
  4. 4. 3. 线程间通信
,