blob: 99bef4bd8aded6ca067b3e9e145b8537df0dcc9e (
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
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
|
package de.danoeh.antennapod.miroguide.conn;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
/** Executes HTTP requests and returns the results. */
public class MiroGuideConnector {
private HttpClient httpClient;
private static final String HOST_URL = "http://www.miroguide.com/api/";
private static final String PATH_GET_CHANNELS = "get_channels";
private static final String PATH_LIST_CATEGORIES = "list_categories";
private static final String PATH_GET_CHANNEL = "get_channel";
public MiroGuideConnector() {
httpClient = new DefaultHttpClient();
}
public void shutdown() {
httpClient.getConnectionManager().shutdown();
}
private Uri.Builder getBaseURIBuilder(String path) {
Uri.Builder builder = Uri.parse(HOST_URL).buildUpon();
builder.appendPath(path).appendQueryParameter("datatype", "json");
return builder;
}
public JSONArray getArrayResponse(Uri uri) throws MiroGuideException {
try {
JSONArray result = new JSONArray(executeRequest(uri));
return result;
} catch (JSONException e) {
e.printStackTrace();
throw new MiroGuideException();
}
}
public JSONObject getSingleObjectResponse(Uri uri) throws MiroGuideException {
try {
JSONObject result = new JSONObject(executeRequest(uri));
return result;
} catch (JSONException e) {
e.printStackTrace();
throw new MiroGuideException();
}
}
/**
* Executes a HTTP GET request with the given URI and returns the content of
* the return value.
*
* @throws MiroGuideException
*/
private String executeRequest(Uri uri) throws MiroGuideException {
HttpGet httpGet = new HttpGet(uri.toString());
String result = null;
try {
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
result = reader.readLine();
in.close();
}
} else {
throw new MiroGuideException(response.getStatusLine()
.getReasonPhrase());
}
} catch (IOException e) {
e.printStackTrace();
throw new MiroGuideException(e.getMessage());
}
return result;
}
public Uri createGetChannelsUri(String filter, String filterValue,
String sort, String limit, String offset) throws MiroGuideException {
Uri.Builder resultBuilder = getBaseURIBuilder(PATH_GET_CHANNELS);
resultBuilder.appendQueryParameter("filter", filter)
.appendQueryParameter("filter_value", filterValue);
if (sort != null) {
resultBuilder.appendQueryParameter("sort", sort);
}
if (limit != null) {
resultBuilder.appendQueryParameter("limit", limit);
}
if (offset != null) {
resultBuilder.appendQueryParameter("offset", offset);
}
Uri result = resultBuilder.build();
return result;
}
public Uri createListCategoriesURI() throws MiroGuideException {
Uri.Builder resultBuilder = getBaseURIBuilder(PATH_LIST_CATEGORIES);
Uri result = resultBuilder.build();
return result;
}
public Uri createGetChannelUri(String id) throws MiroGuideException {
Uri.Builder resultBuilder = getBaseURIBuilder(PATH_GET_CHANNEL)
.appendQueryParameter("id", id);
Uri result = resultBuilder.build();
return result;
}
}
|