1、post請求的工具類
2、線程池的封裝類
3、發送請求的接口類
ps:數據結構的封裝本文暫不涉及
1、post請求的工具類
1 package com.utils;
2
3 /**
5 * http通信的工具類
6 * 支持post類型
7 */
8
9 import java.net.HttpURLConnection;
10 import java.net.URL;
11 import java.io.OutputStream;
12 import java.io.InputStream;
13 import java.util.Map;
14 import java.io.IOException;
15 import java.net.URLEncoder;
16 import java.io.ByteArrayOutputStream;
17
18 public class HttpUtils {
19 /*
20 * Function : 發送Post請求到服務器
21 * Param : params請求體內容,encode編碼格式
22 */
23 public static String submitPostData(String strUrlPath,Map<String, String> params, String encode) {
24
25 byte[] data = getRequestData(params, encode).toString().getBytes();//獲得請求體
26 try {
27 URL url = new URL(strUrlPath);
28
29 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
30 httpURLConnection.setConnectTimeout(3000); //設置連接超時時間
31 httpURLConnection.setDoInput(true); //打開輸入流,以便從服務器獲取數據
32 httpURLConnection.setDoOutput(true); //打開輸出流,以便向服務器提交數據
33 httpURLConnection.setRequestMethod("POST"); //設置以Post方式提交數據
34 httpURLConnection.setUseCaches(false); //使用Post方式不能使用緩存
35 //設置請求體的類型是文本類型
36 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
37 //設置請求體的長度
38 httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
39 //獲得輸出流,向服務器寫入數據
40 OutputStream outputStream = httpURLConnection.getOutputStream();
41 outputStream.write(data);
42
43 int response = httpURLConnection.getResponseCode(); //獲得服務器的響應碼
44 if(response == HttpURLConnection.HTTP_OK) {
45 InputStream inptStream = httpURLConnection.getInputStream();
46 return dealResponseResult(inptStream); //處理服務器的響應結果
47 }
48 } catch (IOException e) {
49 e.printStackTrace();
50 return "err: " + e.getMessage().toString();
51 }
52 return "-1";
53 }
54
55 /*
56 * Function : 封裝請求體信息
57 * Param : params請求體內容,encode編碼格式
58 */
59 public static StringBuffer getRequestData(Map<String, String> params, String encode) {
60 StringBuffer stringBuffer = new StringBuffer(); //存儲封裝好的請求體信息
61 try {
62 for(Map.Entry<String, String> entry : params.entrySet()) {
63 stringBuffer.append(entry.getKey())
64 .append("=")
65 .append(URLEncoder.encode(entry.getValue(), encode))
66 .append("&");
67 }
68 stringBuffer.deleteCharAt(stringBuffer.length() - 1); //刪除最后的一個"&"
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72 return stringBuffer;
73 }
74
75 /*
76 * Function : 處理服務器的響應結果(將輸入流轉化成字符串)
77 * Param : inputStream服務器的響應輸入流
78 */
79 public static String dealResponseResult(InputStream inputStream) {
80 String resultData = null; //存儲處理結果
81 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
82 byte[] data = new byte[1024];
83 int len = 0;
84 try {
85 while((len = inputStream.read(data)) != -1) {
86 byteArrayOutputStream.write(data, 0, len);
87 }
88 } catch (IOException e) {
89 e.printStackTrace();
90 }
91 resultData = new String(byteArrayOutputStream.toByteArray());
92 return resultData;
93 }
94
95
96 }
97
2、線程池的封裝類
1 package com.utils;
2
3 import java.util.concurrent.ExecutorService;
4 import java.util.concurrent.Executors;
5
6 /**
8 * 統一控制系統交互的線程池類
9 */
10
11 public class HttpThreadPool {
12
13 static private ExecutorService executorService = Executors.newCachedThreadPool();
14
15 static public void call(Runnable runnable)
16 {
17 executorService.execute(runnable);
18 }
19 }
20
3、發送請求的接口類
1 package com.inter;
2
7
8 /**
11 */
12
13 public class CMTestIF {
14
15 private CMTestDriver mDriver = null;
16
17 void uploadCmTest(CMTestReq req, HttpResultListener resultListener)
18 {
19 mDriver = new CMTestDriver(req, resultListener);
20 HttpThreadPool.call(mDriver);
21 }
22 }
23
HttpResultListener 是自定義的接口類,CMTestRe是自定義的數據結構
4、調用post函數的業務函數
1 package com.zlc.mycatvtest.driver.systemDriver;
2
3 import com.zlc.mycatvtest.listener.HttpResultListener;
4 import com.zlc.mycatvtest.system.request.CMTestReq;
5 import com.zlc.mycatvtest.utils.HttpUtils;
6
7 /**
10 */
11
12 public class CMTestDriver implements Runnable{
13 private static final String CMT_SERVER_ADDRESS = "http://192.168.1.75/testProject/index.php";
14
15 CMTestReq mReq = null;
16 HttpResultListener mResultListener = null;
17
18
19 public CMTestDriver(CMTestReq req, HttpResultListener resultListener)
20 {
21 mReq = req;
22 mResultListener = resultListener;
23 }
24
25
26 @Override
27 public void run() {
28
29 //服務器請求路徑
30 String strResult= HttpUtils.submitPostData(CMT_SERVER_ADDRESS, mReq.getParas(), "utf-8");
31 34
35 mResultListener.finish(0, strResult);
36
37 }
38 }
39
mReq.getParas()是自定義的函數,返回了Map<String,String>的結構,參考如下函數定義
1 package com.system;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 /**
9 */
10
11 public class CMDownInfo {
12
13 public static final String CMD_FILED_FRQ = "downInfoFrq";
14 …… ……
21 …… ……
22 public static final String CMD_FILED_POST_BER = "downInfoPostBer";
23
24 private long frequency = 0;
…… ……
36
37 public Map<String, String> getParas()
38 {
39 Map<String,String> params = new HashMap<String,String>();
40 params.put(CMD_FILED_FRQ, String.valueOf(this.getFrequency()));
41 ……
48 ……
49 params.put(CMD_FILED_POST_BER, String.valueOf(this.getPostBer()));
50
51 return params;
52 }
53
54 public double getFlt() {
55 return flt;
56 }
57
58 ……
130 public double getPower() {
131 return power;
132 }
133 }
134