-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestJsonClient.java
92 lines (82 loc) · 2.46 KB
/
RestJsonClient.java
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
package com.phonegap.plugin;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import android.util.Log;
public class RestJsonClient {
public static ArrayList<GeoName> getWikipediaNearbyLocations(
double latitude, double longitude) {
HttpURLConnection urlConnection = null;
ArrayList<GeoName> geoList = new ArrayList<GeoName>();
try {
String requestUrl = "http://ws.geonames.org/findNearbyWikipediaJSON?formatted=true&";
requestUrl += "lat=" + latitude + "&";
requestUrl += "lng=" + longitude + "&";
requestUrl += "username=wikimedia";
//Log.d("RestJsonClient", requestUrl);
URL url = new URL(requestUrl);
urlConnection = (HttpURLConnection) url.openConnection();
String jsonStr = convertStreamToString(urlConnection
.getInputStream());
//Log.d("RestJsonClient", jsonStr);
// getting data and if we don't we just get out of here!
JSONArray geonames = null;
try {
JSONObject json = new JSONObject(jsonStr);
geonames = json.getJSONArray("geonames");
} catch (JSONException e) {
e.printStackTrace();
return null;
}
for (int i = 0; i < geonames.length(); i++) {
try {
JSONObject geonameObj = geonames.getJSONObject(i);
geoList.add(new GeoName(
geonameObj.getString("wikipediaUrl"), geonameObj
.getString("title"), geonameObj
.getString("summary"), geonameObj
.getDouble("lat"), geonameObj
.getDouble("lng")));
} catch(JSONException e) {
// ignore exception and keep going!
e.printStackTrace();
}
}
return geoList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return geoList;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}