// AccuWeather extension for App Inventor by Marco Perrone
package AccuWeather;
import com.google.appinventor.components.runtime.*;
import android.app.Activity;
import android.content.Intent;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.annotations.UsesPermissions;
import com.google.appinventor.components.annotations.UsesActivities;
import com.google.appinventor.components.annotations.UsesLibraries;
import com.google.appinventor.components.annotations.UsesActivityMetadata;
import com.google.appinventor.components.annotations.androidmanifest.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.PropertyTypeConstants;
import com.google.appinventor.components.common.YaVersion;
import com.google.appinventor.components.runtime.util.GingerbreadUtil;
import com.google.appinventor.components.runtime.util.SdkLevel;
import com.google.appinventor.components.runtime.util.AsynchUtil;
import com.google.appinventor.components.runtime.util.YailList;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.CookieHandler;
import java.util.Scanner;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.OutputStream;
@SimpleObject(external=true)
@UsesPermissions(permissionNames = "android.permission.INTERNET")
@DesignerComponent(version = YaVersion.WEB_COMPONENT_VERSION,
description = "
Non-visible component to search Weather information from all over the world.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "images/nearfield.png")
public class AccuWeather extends AndroidNonvisibleComponent implements Component {
private final Activity activity;
private final CookieHandler cookieHandler;
private String twoHyphens = "--";
private String boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****";
private String LINE = "\r\n";
private URL url;
private HttpURLConnection httpConn = null;
private OutputStream outputStream;
private PrintWriter writer;
private String charset = "utf-8";
@SimpleEvent(description = "Got City")
public void GotCity(int responseCode, String response, YailList locationKey, YailList city, YailList area, String message) {
EventDispatcher.dispatchEvent(this, "GotCity", responseCode, response, locationKey, city, area, message);
}
@SimpleEvent(description = "Got Forecast")
public void GotForecast(int responseCode, String response, String textForecast, YailList date, YailList tempMin, YailList tempMax, YailList dayIcon, YailList dayPhrase, YailList nightIcon, YailList nightPhrase, String message) {
EventDispatcher.dispatchEvent(this, "GotForecast", responseCode, response, textForecast, date, tempMin, tempMax, dayIcon, dayPhrase, nightIcon, nightPhrase, message);
}
@SimpleEvent(description = "Got Index")
public void GotIndex(int responseCode, String response, YailList name, YailList localDateTime, YailList value, YailList text, String message) {
EventDispatcher.dispatchEvent(this, "GotIndex", responseCode, response, name, localDateTime, value, text, message);
}
@SimpleEvent(description = "Got Current Conditions")
public void GotCurrentConditions(int responseCode, String response, String weather, String icon, String message) {
EventDispatcher.dispatchEvent(this, "GotCurrentConditions", responseCode, response, weather, icon, message);
}
@SimpleEvent(description = "Got Hourly Forecast")
public void GotHourlyForecast(int responseCode, String response, YailList dateTime, YailList icon, YailList text, YailList temp, String message) {
EventDispatcher.dispatchEvent(this, "GotHourlyForecast", responseCode, response, dateTime, icon, text, temp, message);
}
@SimpleFunction(description = "City Search Text")
public void CitySearchText(final String apiKey, final String city, final String language) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/locations/v1/cities/search?";
URLStr = URLStr + "apikey=" + apiKey;
URLStr = URLStr + "&q=" + city;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
CitySearch_Exec(URLStr);
}
});
}
@SimpleFunction(description = "City Search Geoposition")
public void CitySearchGeoPos(final String apiKey, final String lat, final String lon, final String language) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?";
URLStr = URLStr + "apikey=" + apiKey;
URLStr = URLStr + "&q=" + lat + "," + lon;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
CitySearch_Exec(URLStr);
}
});
}
@SimpleFunction(description = "1 Day Forecast")
public void Forecast1Day(final String apiKey, final String locationKey, final String language, final String metric) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/forecasts/v1/daily/1day/" + locationKey + "?";
URLStr = URLStr + "apikey=" + apiKey;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
if (metric.length() == 0)
{
URLStr = URLStr + "&metric=true";
}
else
{
URLStr = URLStr + "&metric=" + metric;
}
Forecast_Exec(URLStr);
}
});
}
@SimpleFunction(description = "5 Day Forecast")
public void Forecast5Day(final String apiKey, final String locationKey, final String language, final String metric) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/forecasts/v1/daily/5day/" + locationKey + "?";
URLStr = URLStr + "apikey=" + apiKey;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
if (metric.length() == 0)
{
URLStr = URLStr + "&metric=true";
}
else
{
URLStr = URLStr + "&metric=" + metric;
}
Forecast_Exec(URLStr);
}
});
}
@SimpleFunction(description = "1 Day of Daily Index")
public void Index1Day(final String apiKey, final String locationKey, final String index, final String language) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/indices/v1/daily/1day/" + locationKey + "/" + index + "?";
URLStr = URLStr + "apikey=" + apiKey;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
URLStr = URLStr + "&details=true";
Indice_Exec(URLStr);
}
});
}
@SimpleFunction(description = "5 Days of Daily Index")
public void Index5Day(final String apiKey, final String locationKey, final String index, final String language) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/indices/v1/daily/5day/" + locationKey + "/" + index + "?";
URLStr = URLStr + "apikey=" + apiKey;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
URLStr = URLStr + "&details=true";
Indice_Exec(URLStr);
}
});
}
@SimpleFunction(description = "Current Conditions")
public void CurrentConditions(final String apiKey, final String locationKey, final String language) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/currentconditions/v1/" + locationKey + "?";
URLStr = URLStr + "apikey=" + apiKey;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
URLStr = URLStr + "&details=false";
CurrentConditions_Exec(URLStr);
}
});
}
@SimpleFunction(description = "12 Hours of Hourly Forecasts")
public void Hourly12Forecast(final String apiKey, final String locationKey, final String language, final String metric) {
AsynchUtil.runAsynchronously(new Runnable() {
@Override
public void run() {
String URLStr = "http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/" + locationKey + "?";
URLStr = URLStr + "apikey=" + apiKey;
if (language.length() == 0)
{
URLStr = URLStr + "&language=en-US";
}
else
{
URLStr = URLStr + "&language=" + language;
}
if (metric.length() == 0)
{
URLStr = URLStr + "&metric=true";
}
else
{
URLStr = URLStr + "&metric=" + metric;
}
HourlyForecast_Exec(URLStr);
}
});
}
public AccuWeather(ComponentContainer container) {
super(container.$form());
activity = container.$context();
cookieHandler = (SdkLevel.getLevel() >= SdkLevel.LEVEL_GINGERBREAD)
? GingerbreadUtil.newCookieManager()
: null;
}
void CitySearch_Exec(String URLStr) {
try
{
InputStream inputStream = null;
URL url = new URL(URLStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
final int status = httpConn.getResponseCode();
List city = new ArrayList();
List area = new ArrayList();
List locationKey = new ArrayList();
String Msg = "";
if (status == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
String ImportantValues = "";
String cityStr = "";
String areaStr = "";
String locationKeyStr = "";
if (responseStr.indexOf("Version") != -1)
{
ImportantValues = responseStr;
while (ImportantValues.indexOf("Version") != -1)
{
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Version")+7);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Key")+6);
locationKeyStr = ImportantValues.substring(0,ImportantValues.indexOf("\""));
locationKey.add(locationKeyStr);
cityStr = ImportantValues.substring(ImportantValues.indexOf("LocalizedName")+16);
cityStr = cityStr.substring(0,cityStr.indexOf("\""));
city.add(cityStr);
areaStr = ImportantValues.substring(ImportantValues.indexOf("AdministrativeArea")+18);
areaStr = areaStr.substring(areaStr.indexOf("LocalizedName")+16);
areaStr = areaStr.substring(0,areaStr.indexOf("\","));
area.add(areaStr);
}
}
else
{
if (responseStr.indexOf("message") != -1)
{
Msg = responseStr.substring(responseStr.indexOf("message")+10);
Msg = Msg.substring(0,Msg.indexOf("\""));
}
else
{
Msg = "No results found";
}
}
httpConn.disconnect();
final List cityFin = city;
final List areaFin = area;
final List locatioKeyFin = locationKey;
final String Message = Msg;
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotCity(status, responseStr , YailList.makeList(locatioKeyFin), YailList.makeList(cityFin), YailList.makeList(areaFin), Message);
}
});
}
else
{
InputStream inputStreamErr = httpConn.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
httpConn.disconnect();
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotCity(status, responseStr , null, null, null, "");
}
});
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
appendLog(errors.toString());
}
}
void Forecast_Exec(String URLStr) {
try
{
InputStream inputStream = null;
URL url = new URL(URLStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
final int status = httpConn.getResponseCode();
List date = new ArrayList();
List tempMin = new ArrayList();
List tempMax = new ArrayList();
List dayIcon = new ArrayList();
List dayPhrase = new ArrayList();
List nightIcon = new ArrayList();
List nightPhrase = new ArrayList();
String TextForecast = "";
String Msg = "";
if (status == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
String ImportantValues = "";
String dateStr = "";
String tempMinStr = "";
String tempMaxStr = "";
String dayIconStr = "";
String dayPhraseStr = "";
String nightIconStr = "";
String nightPhraseStr = "";
if (responseStr.indexOf("Text") != -1)
{
ImportantValues = responseStr;
TextForecast = ImportantValues.substring(ImportantValues.indexOf("Text")+7);
TextForecast = TextForecast.substring(0,TextForecast.indexOf("\""));
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("DailyForecasts"));
while (ImportantValues.indexOf("Date") != -1)
{
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Date"));
dateStr = ImportantValues.substring(ImportantValues.indexOf("Date")+7);
dateStr = dateStr.substring(0,dateStr.indexOf(","));
date.add(dateStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Minimum")+7);
tempMinStr = ImportantValues.substring(ImportantValues.indexOf("Value")+7);
tempMinStr = tempMinStr.substring(0,tempMinStr.indexOf(","));
tempMin.add(tempMinStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Maximum")+7);
tempMaxStr = ImportantValues.substring(ImportantValues.indexOf("Value")+7);
tempMaxStr = tempMaxStr.substring(0,tempMaxStr.indexOf(","));
tempMax.add(tempMaxStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Day")+3);
dayIconStr = ImportantValues.substring(ImportantValues.indexOf("Icon")+6);
dayIconStr = dayIconStr.substring(0,dayIconStr.indexOf(","));
dayIcon.add(dayIconStr);
dayPhraseStr = ImportantValues.substring(ImportantValues.indexOf("IconPhrase")+13);
dayPhraseStr = dayPhraseStr.substring(0,dayPhraseStr.indexOf("\""));
dayPhrase.add(dayPhraseStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Night")+8);
nightIconStr = ImportantValues.substring(ImportantValues.indexOf("Icon")+6);
nightIconStr = nightIconStr.substring(0,nightIconStr.indexOf(","));
nightIcon.add(nightIconStr);
nightPhraseStr = ImportantValues.substring(ImportantValues.indexOf("IconPhrase")+13);
nightPhraseStr = nightPhraseStr.substring(0,nightPhraseStr .indexOf("\""));
nightPhrase.add(nightPhraseStr);
}
}
else
{
if (responseStr.indexOf("message") != -1)
{
Msg = responseStr.substring(responseStr.indexOf("message")+10);
Msg = Msg.substring(0,Msg.indexOf("\""));
}
else
{
Msg = "No results found";
}
}
httpConn.disconnect();
final List dateFin = date;
final List tempMinFin = tempMin;
final List tempMaxFin = tempMax;
final List dayIconFin = dayIcon;
final List dayPhraseFin = dayPhrase;
final List nightIconFin = nightIcon;
final List nightPhraseFin = nightPhrase;
final String textForecast = TextForecast;
final String Message = Msg;
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotForecast(status, responseStr , textForecast, YailList.makeList(dateFin), YailList.makeList(tempMinFin), YailList.makeList(tempMaxFin), YailList.makeList(dayIconFin), YailList.makeList(dayPhraseFin), YailList.makeList(nightIconFin), YailList.makeList(nightPhraseFin), Message);
}
});
}
else
{
InputStream inputStreamErr = httpConn.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
httpConn.disconnect();
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotForecast(status, responseStr , "", null, null, null, null, null, null, null, "");
}
});
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
appendLog(errors.toString());
}
}
void Indice_Exec(String URLStr) {
try
{
InputStream inputStream = null;
URL url = new URL(URLStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
final int status = httpConn.getResponseCode();
List name = new ArrayList();
List localDateTime = new ArrayList();
List value = new ArrayList();
List textIndex = new ArrayList();
String Msg = "";
if (status == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
String ImportantValues = "";
String nameStr = "";
String localDateTimeStr = "";
String valueStr = "";
String textIndexStr = "";
if (responseStr.indexOf("Name") != -1)
{
ImportantValues = responseStr;
while (ImportantValues.indexOf("Name") != -1)
{
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Name"));
nameStr = ImportantValues.substring(ImportantValues.indexOf("Name")+7);
nameStr = nameStr.substring(0,nameStr.indexOf("\""));
name.add(nameStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("LocalDateTime"));
localDateTimeStr = ImportantValues.substring(ImportantValues.indexOf("LocalDateTime")+16);
localDateTimeStr = localDateTimeStr.substring(0,localDateTimeStr.indexOf("\""));
localDateTime.add(localDateTimeStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Value"));
valueStr = ImportantValues.substring(ImportantValues.indexOf("Value")+7);
valueStr = valueStr.substring(0,valueStr.indexOf(","));
value.add(valueStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Text"));
textIndexStr = ImportantValues.substring(ImportantValues.indexOf("Text")+7);
textIndexStr = textIndexStr.substring(0,textIndexStr.indexOf("\""));
textIndex.add(textIndexStr);
}
}
else
{
if (responseStr.indexOf("message") != -1)
{
Msg = responseStr.substring(responseStr.indexOf("message")+10);
Msg = Msg.substring(0,Msg.indexOf("\""));
}
else
{
Msg = "No results found";
}
}
httpConn.disconnect();
final List nameFin = name;
final List localDateTimeFin = localDateTime;
final List valueFin = value;
final List textIndexFin = textIndex;
final String Message = Msg;
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotIndex(status, responseStr , YailList.makeList(nameFin), YailList.makeList(localDateTimeFin), YailList.makeList(valueFin), YailList.makeList(textIndexFin), Message);
}
});
}
else
{
InputStream inputStreamErr = httpConn.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
httpConn.disconnect();
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotIndex(status, responseStr, null, null, null, null, "");
}
});
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
appendLog(errors.toString());
}
}
void CurrentConditions_Exec(String URLStr) {
try
{
InputStream inputStream = null;
URL url = new URL(URLStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
final int status = httpConn.getResponseCode();
String ImportantValues = "";
String weatherStr = "";
String iconStr = "";
String Msg = "";
if (status == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
if (responseStr.indexOf("WeatherText") != -1)
{
ImportantValues = responseStr;
while (ImportantValues.indexOf("WeatherText") != -1)
{
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("WeatherText"));
weatherStr = ImportantValues.substring(ImportantValues.indexOf("WeatherText")+14);
weatherStr = weatherStr.substring(0,weatherStr.indexOf("\""));
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("WeatherIcon"));
iconStr = ImportantValues.substring(ImportantValues.indexOf("WeatherIcon")+13);
iconStr = iconStr.substring(0,iconStr.indexOf(","));
}
}
else
{
if (responseStr.indexOf("message") != -1)
{
Msg = responseStr.substring(responseStr.indexOf("message")+10);
Msg = Msg.substring(0,Msg.indexOf("\""));
}
else
{
Msg = "No results found";
}
}
httpConn.disconnect();
final String weatherFin = weatherStr;
final String iconFin = iconStr;
final String Message = Msg;
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotCurrentConditions(status, responseStr , weatherFin, iconFin, Message);
}
});
}
else
{
InputStream inputStreamErr = httpConn.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
httpConn.disconnect();
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotCurrentConditions(status, responseStr, "", "", "");
}
});
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
appendLog(errors.toString());
}
}
void HourlyForecast_Exec(String URLStr) {
try
{
InputStream inputStream = null;
URL url = new URL(URLStr);
httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
final int status = httpConn.getResponseCode();
List dateTime = new ArrayList();
List icon = new ArrayList();
List text = new ArrayList();
List temp = new ArrayList();
String Msg = "";
if (status == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
String ImportantValues = "";
String dateTimeStr = "";
String iconStr = "";
String textStr = "";
String tempStr = "";
if (responseStr.indexOf("DateTime") != -1)
{
ImportantValues = responseStr;
while (ImportantValues.indexOf("DateTime") != -1)
{
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("DateTime"));
dateTimeStr = ImportantValues.substring(ImportantValues.indexOf("DateTime")+11);
dateTimeStr = dateTimeStr.substring(0,dateTimeStr.indexOf("\""));
dateTime.add(dateTimeStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("WeatherIcon"));
iconStr = ImportantValues.substring(ImportantValues.indexOf("WeatherIcon")+13);
iconStr = iconStr.substring(0,iconStr.indexOf(","));
icon.add(iconStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("IconPhrase"));
textStr = ImportantValues.substring(ImportantValues.indexOf("IconPhrase")+13);
textStr = textStr.substring(0,textStr.indexOf("\""));
text.add(textStr);
ImportantValues = ImportantValues.substring(ImportantValues.indexOf("Value"));
tempStr = ImportantValues.substring(ImportantValues.indexOf("Value")+7);
tempStr = tempStr.substring(0,tempStr.indexOf(","));
temp.add(tempStr);
}
}
else
{
if (responseStr.indexOf("message") != -1)
{
Msg = responseStr.substring(responseStr.indexOf("message")+10);
Msg = Msg.substring(0,Msg.indexOf("\""));
}
else
{
Msg = "No results found";
}
}
httpConn.disconnect();
final List dateTimeFin = dateTime;
final List iconFin = icon;
final List textFin = text;
final List tempFin = temp;
final String Message = Msg;
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotHourlyForecast(status, responseStr , YailList.makeList(dateTimeFin), YailList.makeList(iconFin), YailList.makeList(textFin), YailList.makeList(tempFin), Message);
}
});
}
else
{
InputStream inputStreamErr = httpConn.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
String inputLine;
final StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
final String responseStr = response.toString();
httpConn.disconnect();
// Dispatch the event.
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
GotHourlyForecast(status, responseStr, null, null, null, null, "");
}
});
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
appendLog(errors.toString());
}
}
public void appendLog(String text)
{
File logFile = new File("sdcard/AccuWeatherLog.txt");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.flush();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}