-2

Get city information from wikipedia, and show it on an Android APP.

However, every time I try to transform the data to JSON, throws an exception

https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Threadless&rvprop=content&format=json&rvsection=0

url = new URL("https://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Threadless&rvprop=content&format=json&rvsection=0");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
String line ="";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
sb = new StringBuilder();
while ((line = reader.readLine()) != null){
    sb.append(line);
}
JSONObject city;
JSONArray jsondata = new JSONArray(sb.toString());
city = jsondata.getJSONObject(0);
svick
  • 236,525
  • 50
  • 385
  • 514
Bruno
  • 131
  • 3
  • 17

2 Answers2

0

The Web Service does not return a JSON array, but a JSON Object containing an Array. Try using a JSONObject instead.

JSONObject jsondata = new JSONObject(sb.toString());
Jonas Köritz
  • 2,606
  • 21
  • 33
0

try something like this...

JSONObject searchJson = new JSONObject(Content);
                JSONObject queryObject = searchJson.getJSONObject("query");
                JSONArray searchObject = queryObject.getJSONArray("search");
                JSONObject titObject = (JSONObject) searchObject.get(0);
Shivangi Agrawal
  • 141
  • 1
  • 10