获取维基百科城市信息

时间:2016-04-20 14:49:42

标签: java android api rest wikipedia-api

从维基百科获取城市信息,并在Android APP上显示。

但是,每次我尝试将数据转换为JSON时,都会抛出异常

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);

2 个答案:

答案 0 :(得分:0)

Web Service不返回JSON数组,而是返回包含Array的JSON对象。请尝试使用JSONObject

JSONObject jsondata = new JSONObject(sb.toString());

答案 1 :(得分:0)

尝试这样的事情......

JSONObject searchJson = new JSONObject(Content);
                JSONObject queryObject = searchJson.getJSONObject("query");
                JSONArray searchObject = queryObject.getJSONArray("search");
                JSONObject titObject = (JSONObject) searchObject.get(0);