-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHttpUtilNew.java
170 lines (150 loc) · 6.35 KB
/
HttpUtilNew.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
package com.entrancz.utils;
import android.content.Context;
import android.util.Log;
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.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* Created by jit on 3/30/2016.
*/
public class HttpUtilNew {
public static String makeApiGETRequest(String url, Context context) {
String result ="";
String charset = "UTF-8";
try {
URL urlObj = new URL(url);
HttpURLConnection http_con = (HttpURLConnection) urlObj.openConnection();
http_con.setDoOutput(false);
// http_con.setRequestProperty("Content-Type", "application/json");
/* http_con.setRequestProperty("session_id", PrefUtil.getData("token", context));
http_con.setRequestProperty("Authorization", PrefUtil.getData("token", context));*/
http_con.setRequestProperty("Type", "user");
http_con.setRequestMethod("GET");
http_con.setRequestProperty("Accept-Charset", charset);
// http_con.addRequestProperty("Cache-Control", "max-age=0");
http_con.setConnectTimeout(15000);
http_con.connect();
result = convertStreamToString(http_con, http_con.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String makeApiPOSTRequest(String url, HashMap<String, String> postDataParams, Context context) {
String result = "";
String charset = "UTF-8";
try {
URL urlObj = new URL(url);
HttpURLConnection http_con = (HttpURLConnection) urlObj.openConnection();
http_con.setDoOutput(true);
// http_con.setRequestProperty("Content-Type", "application/json");
// addRequestProperty is not override the old value
/* conn.addRequestProperty("session_id", PrefUtil.getData("token", ctx));
conn.addRequestProperty("user-id", "[email protected]");*/
// setheader it's overrides the old value
/* http_con.setRequestProperty("session_id", PrefUtil.getData("token", context));
http_con.setRequestProperty("user-id", PrefUtil.getData("Email", context));*/
http_con.setRequestMethod("POST");
http_con.setRequestProperty("Accept-Charset", charset);
http_con.setReadTimeout(10000);
http_con.setConnectTimeout(15000);
http_con.connect();
DataOutputStream wr = new DataOutputStream(http_con.getOutputStream());
wr.writeBytes(getPostDataString(postDataParams));
wr.flush();
wr.close();
http_con.getInputStream();
result = convertStreamToString(http_con, http_con.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String makeApiPUTRequest(String url, String paramsString, Context context) {
String result = "";
try {
URL urlObj = new URL(url);
HttpURLConnection http_con = (HttpURLConnection) urlObj.openConnection();
http_con.setDoOutput(true);
//http_con.setRequestProperty("Content-Type", "application/json");
/* http_con.setRequestProperty("Authorization", PrefUtil.getData("token", context));*/
http_con.setRequestProperty("Type", "user");
http_con.setRequestMethod("PUT");
DataOutputStream wr = new DataOutputStream(http_con.getOutputStream());
wr.writeBytes(paramsString);
wr.flush();
wr.close();
result = convertStreamToString(http_con, http_con.getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String makeApiDELETERequest(String url, Context context) {
String result = "";
try {
URL urlObj = new URL(url);
HttpURLConnection http_con = (HttpURLConnection) urlObj.openConnection();
http_con.setRequestProperty("Content-Type", "application/json");
http_con.setRequestMethod("DELETE");
http_con.connect();
try {
result = convertStreamToString(http_con, http_con.getInputStream());
}catch (Exception e){
result = convertStreamToString(http_con, http_con.getErrorStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
public static String convertStreamToString(HttpURLConnection conn, InputStream is) {
StringBuilder result = null;
try {
//Receive the response from the server
InputStream in = new BufferedInputStream(is);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
Log.d(" HttpUtilNew ", "Response result: " + result.toString());
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
return result.toString();
}
}