-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONParser.java
143 lines (110 loc) · 3.92 KB
/
JSONParser.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
package com.ione.iseller;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import javax.net.ssl.HttpsURLConnection;
/**
* Author: Shiv Bhushan Tripathi.
* Date Started: 24/ 03/ 2017.
* Description: Class that handle welcome and login Screen.
* @copyright iOne: A company of Ikai.
*/
class JSONParser {
private String charset = "UTF-8";
private HttpsURLConnection conn;
JSONObject makeHttpRequest(String url, String method,
HashMap<String, String> params) {
StringBuilder sbParams = new StringBuilder();
int i = 0;
for (String key : params.keySet()) {
try {
if (i != 0){
sbParams.append("&");
}
sbParams.append(key).append("=")
.append(URLEncoder.encode(params.get(key), charset));
} catch (UnsupportedEncodingException e) {
return null;
}
i++;
}
URL urlObj;
if (method.equals("POST")) {
// request method is POST
try {
urlObj = new URL(url);
conn = (HttpsURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept-Charset", charset);
// conn.setReadTimeout(15000);
// conn.setConnectTimeout(25000);
conn.connect();
String paramsString = sbParams.toString();
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
} catch (IOException e) {
return null;
}
}
else if(method.equals("GET")){
// request method is GET
if (sbParams.length() != 0) {
url += "?" + sbParams.toString();
}
try {
urlObj = new URL(url);
conn = (HttpsURLConnection) urlObj.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
// conn.setConnectTimeout(25000);
conn.connect();
} catch (IOException e) {
return null;
}
}
try {
StringBuilder result;
if (conn.getResponseCode() == HttpsURLConnection.HTTP_OK) {
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} catch (IOException e) {
return null;
}
conn.disconnect();
JSONObject jObj;
// try parse the string to a JSON object
try {
jObj = new JSONObject(result.toString());
} catch (JSONException e) {
return null;
}
// return JSON Object
return jObj;
} else {
return null;
}
} catch (IOException e) {
return null;
}
}
}