Мой Json:
{
"location":{
"name":"Chennai",
"region":"Tamil Nadu",
"country":"India",
"lat":13.08,
"lon":80.28,
"tz_id":"Asia/Kolkata",
"localtime_epoch":1579921744,
"localtime":"2020-01-25 8:39"
},
"current":{
"last_updated_epoch":1579921212,
"last_updated":"2020-01-25 08:30",
"temp_c":22.0,
"temp_f":71.6,
"is_day":1,
"condition":{
"text":"Mist",
"icon":"//cdn.weatherapi.com/weather/64x64/day/143.png",
"code":1030
},
"wind_mph":0.0,
"wind_kph":0.0,
"wind_degree":0,
"wind_dir":"N",
"pressure_mb":1016.0,
"pressure_in":30.5,
"precip_mm":0.0,
"precip_in":0.0,
"humidity":94,
"cloud":25,
"feelslike_c":24.6,
"feelslike_f":76.2,
"vis_km":2.0,
"vis_miles":1.0,
"uv":5.0,
"gust_mph":6.5,
"gust_kph":10.4
}
}
Мой код
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
String name = jsonObject.getString("location");
Log.i("Weather Details" , name);
String cityname = jsonObject.getString("name");
Log.i("cityname", cityname);
String message = "";
if (!message.equals("")) {
resultTextView.setText(message);
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "couldn't find the giberish you mentioned :(", Toast.LENGTH_SHORT).show();
}
}
Моя ошибка:
0-01-25 09: 28: 41.037 16028-16028 / com.example.my json I / Сведения о погоде: {"name": "Goa", "region": "Goa", "country": "India", "lat": 15,4, "lon" ": 73.8," tz_id ":" Азия / Калькутта "," localtime_epoch ": 1579924225," localtime ":" 2020-01-25 9:20 "}
2020-01-25 09:28: 41.037 16028-16028 / com.example.my json W / System.err: org. json .JSONException: нет значения для имени
2020-01-25 09: 28: 41.037 16028-16028 /com.example.myjson W / System.err: at org. json .JSONObject.get (JSONObject. java: 392)