I'm attempting to build an app that pulls data down from Wunderground.com, and stream the data into an Android app using a ListView. My partner and I have successfully gotten the ListView to work, however streaming the data from Wunderground is causing some problems.
The strange thing is that if I pull the code that gets the data, and paste it into eclipse, it works (sporadically so). I've looked online for solutions, but can't seem to find anything that works. We're trying to pull down the following weather data: humidity, temperature, condition, dateTime, and the image associated with the data.
Below is the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String sURL = "http://freegeoip.net/json/";
URL url = new URL(sURL);//Create a URL object
//Connect to the website with the url variable
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
JsonParser jp = new JsonParser();//Create an object from the JsonParser class
JsonElement root = jp.parse(new BufferedReader(new InputStreamReader((InputStream) request.getContent())));
JsonObject rootobj = root.getAsJsonObject();//create an object for the element
String ZipCode = rootobj.get("zip_code").getAsString();
//Create a string for the web address with the key
String sURL1 = "http://api.wunderground.com/api/1e404159ad02dddd/conditions/hourly/q/" + ZipCode + ".json";
URL url1 = new URL(sURL1);//Create a URL object
//Connect to the website with the url variable
HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
request1.connect();
JsonParser jp1 = new JsonParser();//Create an object from the JsonParser class
JsonElement root1 = jp1.parse(new InputStreamReader((InputStream) request1.getContent()));
JsonObject rootobj1 = root1.getAsJsonObject();//create an object for the element
JsonArray printArray = rootobj1.get("hourly_forecast").getAsJsonArray();
//Add all Wunderground data to the List
for(int i = 0; i < printArray.size(); i++)
{
String dateTime = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("FCTTIME").getAsJsonObject().get("pretty").getAsString();
String condition = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("condition").getAsString();
String temp = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("temp").getAsJsonObject().get("english").getAsString();
String humidity = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("humidity").getAsString();
String image = rootobj1.get("hourly_forecast").getAsJsonArray().get(i).getAsJsonObject().get("icon_url").getAsString();
itemName[i] = "The date is " + dateTime + ". It is " + condition + " outside. It is " + temp + " degrees outside. The humidity is at " + humidity + " percent.";
}
}
catch(Exception e)
{
e.printStackTrace();
}
Any help is appreciated. Thank you.