q_result.optJSONObject("results")
.optJSONObject("channel")
.optJSONObject("item")
.getJSONArray("forecast")
.getJSONObject(1)
.optString("code");
public class Forecast
{
private int code;
private String text;
public int getCode()
{
return code;
}
public String getText()
{
return text;
}
}
public void populate (JSONArray data) throws JSONException
{
code = data.getJSONObject(1).optInt("code");
text = data.getJSONObject(1).optString("text");//The name of the index in the forecast array
}
public class Item
{
private Forecast forecast;
private String title; //This is in the item node, you will want it at some point
public Forecast getForecast()
{
return forecast;
}
public String getTitle()
{
return title;
}
public void populate(JSONObject data) throws JSONException
{
forecast = new Forecast();
forecast.populate(data.getJSONArray("forecast"));//The name of the node in the JSON
title = data.optString("title");
}
}
public class Channel
{
private Item item;
public Item getItem()
{
return item;
}
public void populate(JSONObject data) throws JSONException
{
item = new Item();
item.populate(data.optJSONObject("item"));
}
}
@Override protected void onPostExecute(String result)
{
try
{
JSONObject data = new JSONObject (result);
JSONObject q_result = data.optJSONObject("query");
Channel chan = new Channel();
chan.populate(q_result.optJSONObject("results").optJSONObject("channel"));
main_activity.feed_success(chan);//This is where we use the data
}
catch (JSONException e)
{
main_activity.feed_failure(e);
}
}
public void feed_success(Channel channel) throws JSONException
{
Item item = channel.getItem();
Forecast forecast = item.getForecast();
String forecast_date = forecast.getDate();
String forecast_forc = forecast.getText();
text_view.setText("Tomorrow, " + " " + forecast_date + " will be\n" + forecast_forc);
}
String forecast_date = channel.optJSONObject("results").optJSONObject("channel").optJSONObject("item").getJSONArray("forecast").getJSONObject(1).optString("code");
String forecast_forc = channel.optJSONObject("results").optJSONObject("channel").optJSONObject("item").getJSONArray("forecast").getJSONObject(1).optString("text");
public class Forecast
{
private int code;
private String date;
private String day;
private int high;
private int low;
private String text;
public int getCode()
{
return code;
}
public String getDate()
{
return date;
}
public String getDay()
{
return day;
}
public int getHigh()
{
return high;
}
public int getLow()
{
return low;
}
public String getText()
{
return desc;
}
public void populate (JSONArray data) throws JSONException
{
code = data.getJSONObject(1).optInt("code");
date = data.getJSONObject(1).optString("date");
day = data.getJSONObject(1).optString("day");
high = data.getJSONObject(1).optInt("high");
low = data.getJSONObject(1).optInt("low");
text = data.getJSONObject(1).optString("text");
}
}
public void feed_success(Channel channel) throws JSONException
{
Item item = channel.getItem();
Wind wind = channel.getWind();
Astronomy astron = channel.getAstro();
Units unit = channel.getUnits();
Condition cond = item.getCond();
Forecast forecast = item.getForecast();
String units = unit.getTemp();
String sunrise = astron.getSunrise();
String sunset = astron.getSunset();
String chill = wind.getChill();
String speed = wind.getSpeed();
int cond_code = cond.getCode();
int temp = cond.getTemp();
String desc = cond.getDesc();
String forecast_date = forecast.getDate();
String forecast_day = forecast.getDay();
String forecast_forc = forecast.getDesc();
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)