blob: ffb960140923ad01adc7c9513d129ce4e77d19ff (
plain)
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
|
package uk.co.blatech.blaupload.util;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* This class is used for loading a JSON array from a given URL
*
* TODO: Make the cookie stuff generic
* Created by joe on 02/08/14.
*/
public class JSONLoader extends AsyncTask<String, Void, JSONArray> {
private String url;
/**
*
* @param url - The URL to load JSON from
* @return JSONArray of data from the URL
*/
@Override
protected JSONArray doInBackground(String... url) {
this.url = url[0];
String jsonText = readJSON();
JSONArray json = null;
try {
json = new JSONArray(jsonText);
} catch (JSONException e) {
//TODO: Error handling if we got something other than JSON
Log.e(JSONLoader.class.toString(), "Failed to parse JSON");
e.printStackTrace();
}
return json;
}
private String readJSON() {
StringBuilder builder = new StringBuilder();
DefaultHttpClient client = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
//TODO: Don't hard code the cookie.
BasicClientCookie cookie = new BasicClientCookie("password", "577f7781b7079fd9c0e67ef7e301617cf6eb317311ce8cd0b4b6b2652ffd14d5");
cookie.setDomain(".blaupload.co.uk");
cookie.setPath("/");
cookieStore.addCookie(cookie);
client.setCookieStore(cookieStore);
HttpGet httpGet = new HttpGet(url);
try {
//Read the JSON from the URL
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
//TODO: Error handling if there's no JSON there
Log.e(JSONLoader.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(JSONArray result) {
EventBus.getInstance().post(new JSONLoaderResultEvent(result));
}
}
|