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