Android 来自Wiki的JSon解析
大家好,我如何解析维基百科的描述,我不需要标题或内容,我只需要对维基页面的简短描述。因此,我要修改语法分析器,但有一个错误Android 来自Wiki的JSon解析,android,Android,大家好,我如何解析维基百科的描述,我不需要标题或内容,我只需要对维基页面的简短描述。因此,我要修改语法分析器,但有一个错误 private static String url = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=chocolate"; //names final String QUERY = "query"; final String LI
private static String url = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=chocolate";
//names
final String QUERY = "query";
final String LIST = "search";
final String TITLE = "title";
final String SNIPPET = "snippet";
JSONObject searchJson = new JSONObject(searchJsonStr);
JSONObject queryObject = searchJson.getJSONObject(QUERY);
JSONArray searchObject = queryObject.getJSONArray(LIST);
JSONObject titObject = (JSONObject) searchObject.get(0);
String title = titObject.getString(TITLE);
String description = titObject.getString(SNIPPET);
我的错误是
searchJsonStr无法解析为变量
出现此错误是因为代码中没有任何名为searchJsonStr的变量 您需要首先检索api请求的JSON内容,然后将其用作变量searchJsonStr 您可以使用以下代码,例如:
String searchJsonStr = null;
URL url = new URL("https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=chocolate");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
searchJsonStr = getStringFromInputStream(in);
} else {
searchJsonStr = null;
}
当searchJsonStr初始化时,你能显示代码吗?事实上我在stackoverflow中找到了这段代码,但是没有searchJsonStr,我该怎么做?这不是提问的方式…Biz türkçe de anlaşrız Murat hocam:事实上我在stackoverflow中找到了这段代码,但没有searchJsonStr,我如何做呢?我已经修改了mi-answer,向您展示了如何从API获取JSON的示例