-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebServiceGetTask.java
More file actions
230 lines (187 loc) · 6.48 KB
/
Copy pathWebServiceGetTask.java
File metadata and controls
230 lines (187 loc) · 6.48 KB
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
package com.ntier.android.REST;
import java.io.*;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.*;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.google.gson.Gson;
import com.google.gson.stream.JsonWriter;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class WebServiceGetTask extends AsyncTask<String, Integer, String> {
private Context mContext = null;
private String processMessage = "Processing...";
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
private ProgressDialog pDlg = null;
@SuppressWarnings("hiding")
public WebServiceGetTask(Context mContext, String processMessage) {
this.mContext = mContext;
this.processMessage = processMessage;
}
public void addNameValuePair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}
private void showProgressDialog() {
pDlg = new ProgressDialog(mContext);
pDlg.setMessage(processMessage);
pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDlg.setCancelable(false);
pDlg.show();
}
@Override
protected void onPreExecute() {
//hideKeyboard();
showProgressDialog();
}
protected void trustEveryone(boolean trust) {// FOR TESTING/DEV PURPOSE ONLY!!! HIGHLY INSECURE!!!
// see: http://stackoverflow.com/questions/1217141/self-signed-ssl-acceptance-android/1607997#1607997
if (!trust) return;
try {
HttpsURLConnection
.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname,
SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null,
new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(
X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(
X509Certificate[] chain, String authType)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context
.getSocketFactory());
} catch (Exception e) { // should never happen
e.printStackTrace();
}
}// trustEveryone
@Override
protected String doInBackground(String... urls) {
String result = "";
HttpsURLConnection urlConnection = null;
InputStream in = null;
try {
URL url = new URL(urls[0]);
trustEveryone(true);
urlConnection = (HttpsURLConnection) url.openConnection();
//debugging
InputStream ins = urlConnection.getInputStream();
in = new BufferedInputStream(ins );
result = inputStreamToString(in);
in.close();
} // TODO Auto-generated catch block
catch (IOException e) { e.printStackTrace(); }
finally {
if (urlConnection != null) urlConnection.disconnect();
}
return result;
}//doInBackground
@Override
protected void onPostExecute(String response) {
//send update
Intent intentUpdate = new Intent();
// intentUpdate.setAction(ACTION_MyUpdate);
intentUpdate.addCategory(Intent.CATEGORY_DEFAULT);
// intentUpdate.putExtra(EXTRA_KEY_UPDATE, i);
// sendBroadcast(intentUpdate);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intentUpdate);
//handleResponse(response);
pDlg.dismiss();
}
protected String inputStreamToString(InputStream is) {
StringBuilder total = new StringBuilder();
try {
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(
new InputStreamReader(is));
String line;
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
rd.close();
} catch (IOException e) {
Log.e("TAG", e.getLocalizedMessage(), e);
}
// Return full string
return total.toString();
}//inputStreamToString
}//class WebServiceGetTask
class WebServicePostTask extends WebServiceGetTask{
public Location location = new Location();
public WebServicePostTask(Context mContext, String processMessage) {
super(mContext, processMessage);
location.provider = "from client: " + processMessage;
location.id = 44;
}
@SuppressWarnings("resource")
@Override
protected String doInBackground(String... urls) {
String result = "";
HttpsURLConnection urlConnection = null;
try {
URL url = new URL(urls[0]);
trustEveryone(true);
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setChunkedStreamingMode(0);
urlConnection.setRequestProperty("Content-Type","application/json");
urlConnection.connect();
//debug
OutputStream OUTPUTSTREAM = urlConnection.getOutputStream();
OutputStream out = new BufferedOutputStream(OUTPUTSTREAM);
writeJsonStream(out, this.location);
out.close();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
result = inputStreamToString(in);
}
catch (IOException e) {
e.printStackTrace();
@SuppressWarnings("null")
InputStream errIn = new BufferedInputStream(urlConnection.getErrorStream());
result = inputStreamToString(errIn);
}
finally { if (urlConnection != null) urlConnection.disconnect(); }
return result;
}//doInBackground
public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException {
Gson gson = new Gson();
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out)); //, "UTF-8"));
writer.setIndent(" ");
writer.beginArray();
for (Message message : messages) {
gson.toJson(message, Message.class, writer);
}
writer.endArray();
writer.close();
}
public void writeJsonStream(OutputStream out, Location myloc) throws IOException {
Gson gson = new Gson();
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out)); //, "UTF-8"));
writer.setIndent(" ");
gson.toJson(myloc, Location.class, writer);
writer.close();
}
}//WebServicePostTask