-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.java
358 lines (302 loc) · 9.79 KB
/
Connection.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.TreeMap;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.TextUtils;
public class Connection {
public static final int CONN_MODE_GET = 1;
public static final int CONN_MODE_POST = 2;
private int connMode = 1;
private URL mUrl;
protected Parameter mParameter;
protected byte[] mPostData;
private String mContentType;
private JSONObject errorMsg;
public Connection(URL url, JSONObject errorMsg) {
this.mUrl = url;
this.errorMsg = errorMsg;
}
public void setConnMode(int connMode) {
this.connMode = connMode;
}
public void addParams(Parameter param) {
this.mParameter = param;
}
public void setPostDataBytes(byte[] postData) {
this.mPostData = postData;
}
public void setContentType(String contentType) {
this.mContentType = contentType;
}
public JSONObject requestJson() {
ByteArrayOutputStream bayos = new ByteArrayOutputStream();
NetworkError result = request(bayos);
if (result == NetworkError.OK) {
try {
String json = bayos.toString("UTF-8");
bayos.close();
return new JSONObject(json);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
public String requestString() {
ByteArrayOutputStream bayos = new ByteArrayOutputStream();
NetworkError result = request(bayos);
if (result == NetworkError.OK) {
try {
String str = bayos.toString("UTF-8");
bayos.close();
return str;
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
private NetworkError request(ByteArrayOutputStream bayos) {
String url = "";
if (connMode == CONN_MODE_GET && mParameter != null && !mParameter.isEmpty()) {
String query = mUrl.getQuery();
String baseUrl = mUrl.toString();
String s4;
if (TextUtils.isEmpty(query))
s4 = (new StringBuilder()).append(baseUrl).append("?").append(mParameter.toEncodedString()).toString();
else
s4 = (new StringBuilder()).append(baseUrl).append("&").append(mParameter.toEncodedString()).toString();
url = s4;
return requestInner(url, bayos);
}
return requestInner(mUrl.toString(), bayos);
}
private NetworkError requestInner(String url, ByteArrayOutputStream bayos) {
int statusCodeInt = 0;
try {
URL u = new URL(url);
HttpURLConnection httpurlconnection = (HttpURLConnection) u.openConnection();
httpurlconnection.setConnectTimeout(10000);
if (YummyUtil.isWifiConnected()) {
httpurlconnection.setReadTimeout(10000);
} else {
httpurlconnection.setReadTimeout(30000);
}
if (connMode == CONN_MODE_GET) {
httpurlconnection.setRequestMethod("GET");
httpurlconnection.setDoOutput(false);
} else if (connMode == CONN_MODE_POST) {
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setDoOutput(true);
httpurlconnection.setUseCaches(false);
if (mPostData != null && mPostData.length > 0) {
httpurlconnection.setRequestProperty("Content-Length", Integer.toString(mPostData.length));
}
if (!TextUtils.isEmpty(mContentType)) {
httpurlconnection.setRequestProperty("Content-Type", mContentType);
}
if (mParameter != null && !mParameter.isEmpty()) {
TreeMap<String, String> params = mParameter.getParams();
for (Iterator<String> iterator = params.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
String val = params.get(key);
httpurlconnection.setRequestProperty(key, val);
}
}
}
YummyUtil.printLog("Make Connect : " + url + "\n" + ((mPostData == null) ? "" : new String(mPostData)));
httpurlconnection.connect();
if (mPostData != null && mPostData.length > 0) {
OutputStream outputstream = httpurlconnection.getOutputStream();
outputstream.write(mPostData);
outputstream.close();
}
NetworkError statusCode = handleResponseCode(httpurlconnection.getResponseCode());
statusCodeInt = httpurlconnection.getResponseCode();
YummyUtil.printLog("Make Connect : statusCode(" + httpurlconnection.getResponseCode() + ");");
if (statusCode == NetworkError.OK) {
if (bayos != null) {
BufferedInputStream bufferedinputstream1 = new BufferedInputStream(httpurlconnection.getInputStream(), 8192);
byte[] abyte1 = new byte[1024];
int j;
while ((j = bufferedinputstream1.read(abyte1, 0, 1024)) > 0) {
bayos.write(abyte1, 0, j);
}
bayos.flush();
bufferedinputstream1.close();
}
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
} else {
if (bayos != null) {
BufferedInputStream bufferedinputstream1 = new BufferedInputStream(httpurlconnection.getInputStream(), 8192);
byte[] abyte1 = new byte[1024];
int j;
while ((j = bufferedinputstream1.read(abyte1, 0, 1024)) > 0) {
bayos.write(abyte1, 0, j);
}
bayos.flush();
YummyUtil.printLog("Make Connect : " + url + ", Error Result: " + httpurlconnection.getResponseCode() + ", Response: "
+ bayos.toString("UTF-8"));
bayos.close();
bufferedinputstream1.close();
}
if (httpurlconnection != null) {
httpurlconnection.disconnect();
}
}
return statusCode;
} catch (Exception e) {
e.printStackTrace();
try {
errorMsg.put("errorMsg", ExceptionUtil.getErrorInfoFromException(e) + "\n statusCode(" + statusCodeInt + ")");
} catch (JSONException e1) {
e1.printStackTrace();
}
}
return NetworkError.CLIENT_ERROR;
}
private NetworkError handleResponseCode(int i) {
if (i == 200)
return NetworkError.OK;
if (i == 401) {
return NetworkError.AUTH_ERROR;
} else {
return NetworkError.SERVER_ERROR;
}
}
public enum NetworkError {
OK, URL_ERROR, NETWORK_ERROR, AUTH_ERROR, CLIENT_ERROR, SERVER_ERROR, RESULT_ERROR, UNKNOWN_ERROR
}
private static StringBuilder appendParameter(StringBuilder stringbuilder, String s, String s1) {
if (stringbuilder.length() > 0)
stringbuilder.append("&");
stringbuilder.append(s);
stringbuilder.append("=");
stringbuilder.append(s1);
return stringbuilder;
}
private static StringBuilder appendParameter(StringBuilder stringbuilder, String s, String s1, char c) {
if (stringbuilder.length() > 0)
stringbuilder.append(c);
stringbuilder.append(s);
stringbuilder.append("=");
stringbuilder.append(s1);
return stringbuilder;
}
private static StringBuilder appendParameter(StringBuilder stringbuilder, String s, String s1, String s2) {
if (stringbuilder.length() > 0)
stringbuilder.append("&");
stringbuilder.append(s);
stringbuilder.append("=");
try {
stringbuilder.append(URLEncoder.encode(s1, s2));
} catch (UnsupportedEncodingException unsupportedencodingexception) {
return stringbuilder;
}
return stringbuilder;
}
public class Parameter {
private boolean disallowEmptyValue;
private TreeMap<String, String> params;
public Parameter() {
this(true);
}
public Parameter(boolean bindToConnection) {
params = new TreeMap<String, String>();
if (bindToConnection) {
mParameter = this;
}
}
public Parameter add(String paramString, int paramInt) {
this.params.put(paramString, String.valueOf(paramInt));
return this;
}
public Parameter add(String paramString, Object paramObject) {
if (paramObject == null) {
if (this.disallowEmptyValue)
return this;
paramObject = "";
}
this.params.put(paramString, String.valueOf(paramObject));
return this;
}
public Parameter add(String paramString1, String paramString2) {
if (TextUtils.isEmpty(paramString2)) {
if (this.disallowEmptyValue)
return this;
paramString2 = "";
}
this.params.put(paramString1, paramString2);
return this;
}
public Parameter add(String paramString, boolean paramBoolean) {
this.params.put(paramString, String.valueOf(paramBoolean));
return this;
}
public TreeMap<String, String> getParams() {
return this.params;
}
public boolean isEmpty() {
return this.params.isEmpty();
}
public void setDisallowEmptyValue(boolean paramBoolean) {
this.disallowEmptyValue = paramBoolean;
}
public String toEncodedString() {
return toEncodedString("UTF-8");
}
public String toEncodedString(String paramString) {
if (this.params.isEmpty())
return "";
StringBuilder localStringBuilder = new StringBuilder();
Iterator<String> localIterator = this.params.keySet().iterator();
while (localIterator.hasNext()) {
String str = localIterator.next();
localStringBuilder = Connection.appendParameter(localStringBuilder, str, (String) this.params.get(str), paramString);
}
return localStringBuilder.toString();
}
public String toString() {
return toString('&');
}
public String toString(char paramChar) {
if (this.params.isEmpty())
return "";
StringBuilder localStringBuilder = new StringBuilder();
Iterator<String> localIterator = this.params.keySet().iterator();
while (localIterator.hasNext()) {
String str = (String) localIterator.next();
localStringBuilder = Connection.appendParameter(localStringBuilder, str, (String) this.params.get(str), paramChar);
}
return localStringBuilder.toString();
}
}
}
class ExceptionUtil {
public static String getErrorInfoFromException(Exception e) {
try {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
} catch (Exception e2) {
return "bad getErrorInfoFromException";
}
}
}