-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadJSON.java
More file actions
96 lines (78 loc) · 2.5 KB
/
ReadJSON.java
File metadata and controls
96 lines (78 loc) · 2.5 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
package com.github.jacketapp.app;
import android.os.AsyncTask;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.ContentHandler;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutionException;
/**
* Created by hansrodtang on 28/05/14.
*/
class Message {
public String Content;
public int Code;
Message(String message, int code){
this.Content = message;
this.Code = code;
}
}
class ReadJSON extends AsyncTask<String, Void, Message> {
JSONObject jsonObject;
protected Message doInBackground(String... input) {
// Depends on your web service
//httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
URL url = new URL(input[0]);
InputStream stream = url.openStream();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(stream));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
result = sb.toString();
try {
jsonObject = new JSONObject(result);
Message msg = new Message(jsonObject.getString("message"), jsonObject.getInt("code"));
return msg;
}catch (Exception e){
e.printStackTrace();
}
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return new Message("", 0);
}
protected void onPostExecute(Message msg) {
super.onPostExecute(msg);
}
}