即使我在json数组中有“ feels_like”,您也可以在日志信息中看到错误,但表明它不存在
CODE:
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
String weather = jsonObject.getString("weather");
String temperature = jsonObject.getString("main");
String ans = weather + temperature;
Log.i("ans",ans);
JSONArray arr = new JSONArray(ans);
for (int i=0;i<arr.length();i++){
JSONObject jsonpart = arr.getJSONObject(i);
getweather.setText(jsonpart.getString("feels_like") + "\n" + jsonpart.getString("description"));
}
}catch (Exception e){
e.printStackTrace();
}
}
错误:
D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2020-04-16 08:07:54.896
14696-14696/com.example.myweather I/ans:[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}]{"temp":4.62,"feels_like":1.84,"temp_min":1.67,"temp_max":7.78,"pressure":1017,"humidity":81}
2020-04-16 08:07:54.898 14696-14696/com.example.myweather W/System.err: org.json.JSONException: No value for feels_like
2020-04-16 08:07:54.899 14696-14696/com.example.myweather W/System.err: at org.json.JSONObject.get(JSONObject.java:399)
根据堆栈跟踪,看起来JSON中没有键(feels_like),或者尝试从错误的JSONObject访问。如果您发布JSON结构,可能有助于更好地理解。