1 import android.os.Bundle;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.net.HttpURLConnection;
8 import java.net.URL;
9 import java.net.URLEncoder;
10 import java.util.HashMap;
11 import java.util.Map;
12
13 public class HttpUtils {
14
15 public static void httpGet(String path, HashMap<String, Object> params, CallBack1 callBack1) {
16 InputStream inputStream = null;
17 try {
18 StringBuilder buffer = new StringBuilder();
19 //GET里记得加上?
20 buffer.append("?");
21 if (params != null) {
22 for (Map.Entry<String, Object> entry : params.entrySet()) {
23 buffer.append(entry.getKey())
24 .append("=")
25 .append(URLEncoder.encode(entry.getValue().toString(), "utf-8"))
26 .append("&");
27 }
28 //删去最后一个符号&
29 buffer.deleteCharAt(buffer.length() - 1);
30 }
31 //注意这里的路径是get的方式
32 URL url = new URL(path + buffer.toString());
33 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
34 connection.setRequestMethod("GET");
35 connection.setConnectTimeout(3000);
36 connection.setDoInput(true);
37 int responseCode = connection.getResponseCode();
38 if (responseCode == 200) {
39 inputStream = connection.getInputStream();
40 }
41 //接口回调
42 callBack1.action(inputStream);
43 } catch (Exception e) {
44 e.printStackTrace();
45 } finally {
46 try {
47 if (inputStream != null)
48 inputStream.close();
49 } catch (IOException e) {
50 e.printStackTrace();
51 }
52 }
53 }
54
55 public static void httpPost(String path, HashMap<String, Object> params, CallBack1 callBack1) {
56 InputStream inputStream = null;
57 try {
58 StringBuilder buffer = new StringBuilder();
59 if (params != null) {
60 for (Map.Entry<String, Object> entry : params.entrySet()) {
61 buffer.append(entry.getKey())
62 .append("=")
63 .append(URLEncoder.encode(entry.getValue().toString(), "utf-8"))
64 .append("&");
65 }
66 buffer.deleteCharAt(buffer.length() - 1);
67 }
68 //获取上传信息的字节大小及长度
69 byte[] bytes = buffer.toString().getBytes();
70 URL url = new URL(path);
71 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
72 connection.setRequestMethod("POST");
73 connection.setConnectTimeout(3000);
74 //设置请求体的类型是文本类型
75 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
76 //设置请求体的长度
77 connection.setRequestProperty("Content-Length", String.valueOf(bytes.length));
78 OutputStream outputStream = connection.getOutputStream();
79 //把请求信息写入到输出流当中
80 outputStream.write(bytes);
81 int responseCode = connection.getResponseCode();
82 if (responseCode == 200) {
83 inputStream = connection.getInputStream();
84 }
85 //接口回调
86 callBack1.action(inputStream);
87 } catch (Exception e) {
88 e.printStackTrace();
89 } finally {
90 try {
91 if (inputStream != null)
92 inputStream.close();
93 } catch (IOException e) {
94 e.printStackTrace();
95 }
96 }
97 }
98
99 public static String httpGetString(String path, HashMap<String, Object> params) {
100 final Bundle bundle = new Bundle();
101 httpGet(path, params, new CallBack1() {
102 @Override
103 public void action(InputStream inputStream) {
104 try {
105 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
106 byte[] bytes = new byte[1024];
107 int len;
108 while ((len = inputStream.read(bytes)) != -1) {
109 outputStream.write(bytes, 0, len);
110 }
111 String data = new String(outputStream.toByteArray());
112
113 bundle.putString("data", data);
114 } catch (IOException e) {
115 e.printStackTrace();
116 }
117 }
118 });
119 return bundle.getString("data");
120 }
121
122 public static String httpPostString(String path, HashMap<String, Object> params) {
123 final Bundle bundle = new Bundle();
124 httpPost(path, params, new CallBack1() {
125 @Override
126 public void action(InputStream inputStream) {
127 try {
128 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
129 byte[] bytes = new byte[1024];
130 int len;
131 while ((len = inputStream.read(bytes)) != -1) {
132 outputStream.write(bytes, 0, len);
133 }
134 String data = new String(outputStream.toByteArray());
135
136 bundle.putString("data", data);
137 } catch (IOException e) {
138 e.printStackTrace();
139 }
140 }
141 });
142 return bundle.getString("data");
143 }
144
145 public interface CallBack1 {
146 void action(InputStream inputStream);
147 }
148
149 }