I'm parsing json
data from OpenWeathermap.org which is like this :
{
"cod":"200",
"message":0.0016,
"city":{
"id":1164408,
"name":"Sukkur",
"coord":{
"lon":68.857384,
"lat":27.70516
},
"country":"PK",
"population":0,
"sys":{
"population":0
} },
"cnt":2,
"list":[
{
"dt":1394175600,
"temp":{
"day":26.31,
"min":20,
"max":30.17,
"night":22.71,
"eve":30.17,
"morn":20
},
"pressure":1024.11,
"humidity":59,
"weather":[
{
"id":801,
"main":"Clouds",
"description":"few clouds",
"icon":"02d"
}
],
"speed":0.91,
"deg":121,
"clouds":12
},
{
"dt":1394262000,
"temp":{
"day":25.58,
"min":18.94,
"max":28.22,
"night":21.08,
"eve":28.22,
"morn":18.94
},
"pressure":1026.39,
"humidity":58,
"weather":[
{
"id":800,
"main":"Clear",
"description":"sky is clear",
"icon":"01d"
}
],
"speed":5.75,
"deg":74,
"clouds":0
}
]}
after looking at thisSO Question, I made my Model.java
class for above json
data as
public class Model{
protected String cityId = null;
protected String cityName = null;
protected String countryName = null;
protected String longitude = null;
protected String latitude = null;
protected String polution = null;
protected List<ForecatList> forcastList = null;
// getters setters
public class ForecatList {
protected String dayTimeTemp = null;
protected String maxTemp = null;
protected String minTemp = null;
protected String nightTimeTemp = null;
protected String eveTimeTemp = null;
protected String mornTimeTemp = null;
protected String pressure = null;
protected String humidity = null;
protected String windSpeed = null;
protected String WindDegree = null;
protected String clouds = null;
protected List<Weather> weathers = null;
// getters setters
public class Weather {
protected String weatherId = null;
protected String weatherCondition = null;
protected String weatherDescription = null;
protected String weatherIcon = null;
// getters setters
}
}}
parsing profess is :
public Model getForecastByCityName(Context context, String city, int ofDays){
Model model = null;
try {
Gson gson = new Gson();
Reader reader = new InputStreamReader(forecastByCityName(context, city, ofDays));
model = gson.fromJson(reader, Model.class);
return model;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
and forecastByCityName()
is :
private InputStream forecastByCityName(Context context, String city, int ofDays) throws ClientProtocolException, IOException{
HttpClient httpClient = null;
StringBuilder url = new StringBuilder(URL_FORCAST);
url.append(city);
url.append(String.valueOf(ofDays));
url.append(UNIT);
HttpGet httpRequest = new HttpGet(url.toString());
httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpRequest);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpEntity httpEntity = httpResponse.getEntity();
return httpEntity.getContent();
}else {
return null;
}
}
my request is successful, but when I try to get values from Model
object like :
textView.setText(model.getCityName() + model.getCountryName());
that prints null null null
, why that fails to get values? any idea..