-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONParser.java
More file actions
195 lines (169 loc) · 6.9 KB
/
Copy pathJSONParser.java
File metadata and controls
195 lines (169 loc) · 6.9 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
package project.server;
import android.content.Entity;
import android.os.AsyncTask;
import android.preference.PreferenceActivity;
import android.util.Log;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Larissa Laich on 11.11.14.
*/
public abstract class JSONParser extends AsyncTask<String, Void, JSONObject> {
protected void onPreExecute(){
// Log.d("LogLari","is called before doInBackground");
}
protected void onPostExecute(JSONObject jObj){
// Log.d("LogLari","is called after doInBackground");
if (jObj == null){
//if json could not be loaded
JSONNotLoaded();
}else {
onJSONLoaded(jObj);
}
}
//abstract methods to be implemented in sublcass
public abstract void onJSONLoaded(JSONObject jObj);
public abstract void JSONNotLoaded();
@Override
protected JSONObject doInBackground(String... transfer) {
JSONObject jObj = null;
try {
HttpResponse httpResponse = null;
if(transfer[1].equals("GET")) {
httpResponse = GETResponse(transfer[0]);
}else if(transfer[1].equals("POST")){
httpResponse = POSTResponse(transfer[0]);
}else if(transfer[1].equals("PUT")){
httpResponse = PUTResponse(transfer[0]);
}else if(transfer[1].equals("DELETE")){
httpResponse = DELETEResponse(transfer[0]);
}
// if message entity exits => get it here => in our example feedback json
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line); // if it is a JSONArray: use sb.append(line + "n");
line = reader.readLine();
}
is.close();
String json = sb.toString();
// try parse the string to a JSON object
//[ = JSONArray && { = JSONObject, aufpassen welches Object falsch ist
//JSONObject is an unordered collection of name/value pairs
jObj= new JSONObject(json); // if you get a JSONArray use: jObj= new JSONArray(json);
} catch (Exception e) {
Log.e("LogLari", "Error creating JSON, try jObj= new JSONArray(json);" + e.toString());
return null;
}
return jObj;
}
private HttpResponse GETResponse(String url) throws IOException {
//for GET Methods
HttpGet httpGet = new HttpGet(url);
//utf-8 important for umlaute
//httpGet.setHeader("Content-Type", "text/html; charset=utf-8"); => set this code in your mainActivity
if(addHeader() != null) {
for (Header header : addHeader()) {
httpGet.setHeader(header);
}
}
DefaultHttpClient httpClient = new DefaultHttpClient();
return httpClient.execute(httpGet);
}
private HttpResponse POSTResponse(String url) throws Exception{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
if(addHeader() != null) {
for (Header header : addHeader()) {
post.setHeader(header);
}
}
UrlEncodedFormEntity entity = getUploadContent();
entity = getUploadContent();
if(entity != null) {
post.setEntity(entity);
}
//other idea
//StringEntity jsonEntity = getUploadContentJSON();
//post.setEntity(jsonEntity);
return client.execute(post);
}
private HttpResponse DELETEResponse(String url) throws IOException {
HttpDelete httpDelete = new HttpDelete(url);
DefaultHttpClient httpClient = new DefaultHttpClient();
return httpClient.execute(httpDelete);
}
private HttpResponse PUTResponse(String url) throws Exception {
HttpPut httpPut = new HttpPut(url);
if(addHeader() != null) {
for (Header header : addHeader()) {
httpPut.setHeader(header);
}
}
DefaultHttpClient httpClient = new DefaultHttpClient();
UrlEncodedFormEntity entity = getUploadContent();
entity = getUploadContent();
if(entity != null) {
httpPut.setEntity(entity);
}
return httpClient.execute(httpPut);
}
//Override this method in subclass if you want to post or put so
protected UrlEncodedFormEntity getUploadContent() throws Exception{
Log.e("Tag", "Don't forget to override this method");
return null;
}
//Override this method in subclass if you want to post or put so, other option
//easier to do, if you're working with JSON Objects
protected StringEntity getUploadContentJSON() throws Exception{
Log.e("Tag", "Don't forget to override this method");
//other idea hand JSON within url[2] for this information, e.g jsonString, see last idea
//alternative, post whole dictionary at once
// also very interesting: Gson Java library (can convert Java Objects into their JSON representation or jsonString ot an equivalent Java object.
//String json = new GsonBuilder().create().toJson(comment, Map.class);
//another option:
/*try{
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "test");
jsonObject.put("lastName", "Wabu");
Log.d("LogsLari", "hi" + jsonObject.toString());
StringEntity stringEntity = new StringEntity(jsonObject.toString(),"UTF-8");
stringEntity.setContentType("application/json");
}catch(Exception e){
e.printStackTrace();
}
//return stringEntity;
*/
return null;
}
//Override in subclass if you want to add headers
protected Header[] addHeader(){
Log.e("Tag", "Don't forget to override this method if you want to use headers");
return null;
}
}