在我的CountryInfoActivity.java我有一个从这个网站Async Class检索JSON : https://pt.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal。然后它将节点解析extract为字符串,以便我可以在我的 TextView 中设置它。问题是,每当我在文本视图中设置文本时,我的应用程序就会崩溃。JSON解析是正确的,因为它正在检索我想要的所有信息......
这些是我用来检索数据的类,在最后一个中,我尝试将数据设置textoSobrePais到我的 TextView 中...顺便说一下,在我的onCreate 方法中,我以这种方式调用了该类new DownloadTask().execute(url);
public class DownloadTask extends AsyncTask<String,Integer,Void>{
@Override
protected Void doInBackground(String... params) {
String url = params[0];
getJSONFromURL(url);
return null;
}
}
public String getJSONFromURL(String url){
String json_string = null;
try{
URL obj = new URL(url);
HttpURLConnection http = (HttpURLConnection) obj.openConnection();
http.setRequestMethod("GET");
int response = http.getResponseCode();
Log.i("response",Integer.toString(response));
BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine())!= null){
sb.append(line+"\n");
}
reader.close();
json_string = sb.toString();
Log.i("json_string",json_string);
} catch (UnsupportedEncodingException e){
e.printStackTrace();
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
ParseJson(json_string);
return null;
}
public void ParseJson (String json){
JSONObject obj = null;
try {
obj = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
try {
JSONArray pageIdObj = obj.getJSONObject("query").getJSONObject("pages").names();
String page = String.valueOf(pageIdObj.get(0));
Log.i("ASdasd",page);
textoSobrePais = obj.getJSONObject("query").getJSONObject("pages").getJSONObject(page).getString("extract");
page = "";
Log.i("texte",textoSobrePais);
txtInfoPais = findViewById(R.id.txtInfoPais);
txtInfoPais.setText(textoSobrePais);
} catch (JSONException e) {
e.printStackTrace();
}
}
这是崩溃时给我的错误: https ://pastebin.com/PJh5r36u
有人可以帮忙吗?