From 61d4e34e9da25f6ce3e5fd89c6c0305d8d7ed9cf Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 16 Nov 2014 01:08:31 +0100 Subject: [PATCH] WeatherInformation: service and jpos current and forecast parsers --- .../fragment/current/CurrentFragment.java | 10 +- .../fragment/overview/OverviewFragment.java | 10 +- .../notification/NotificationIntentService.java | 8 +- .../weather/information/parser/IJPOSParser.java | 17 - .../information/parser/JPOSCurrentParser.java | 207 +++++++++++ .../information/parser/JPOSForecastParser.java | 213 +++++++++++ .../information/parser/JPOSWeatherParser.java | 405 --------------------- .../information/service/ServiceCurrentParser.java | 35 ++ .../information/service/ServiceForecastParser.java | 36 ++ .../weather/information/service/ServiceParser.java | 64 ---- .../widget/service/WidgetIntentService.java | 8 +- .../app/src/main/res/raw/licenses.html | 1 + 12 files changed, 510 insertions(+), 504 deletions(-) delete mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/IJPOSParser.java create mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSCurrentParser.java create mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSForecastParser.java delete mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSWeatherParser.java create mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceCurrentParser.java create mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceForecastParser.java delete mode 100644 Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceParser.java diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java index 0ede8d1..ed19276 100644 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/current/CurrentFragment.java @@ -39,10 +39,10 @@ import name.gumartinm.weather.information.httpclient.CustomHTTPClient; import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; import name.gumartinm.weather.information.model.currentweather.Current; -import name.gumartinm.weather.information.parser.JPOSWeatherParser; +import name.gumartinm.weather.information.parser.JPOSCurrentParser; import name.gumartinm.weather.information.service.IconsList; import name.gumartinm.weather.information.service.PermanentStorage; -import name.gumartinm.weather.information.service.ServiceParser; +import name.gumartinm.weather.information.service.ServiceCurrentParser; import name.gumartinm.weather.information.widget.WidgetProvider; public class CurrentFragment extends Fragment { @@ -169,7 +169,7 @@ public class CurrentFragment extends Fragment { final CurrentTask task = new CurrentTask( this.getActivity().getApplicationContext(), new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")), - new ServiceParser(new JPOSWeatherParser())); + new ServiceCurrentParser(new JPOSCurrentParser())); task.execute(weatherLocation.getLatitude(), weatherLocation.getLongitude()); } @@ -464,10 +464,10 @@ public class CurrentFragment extends Fragment { // Store the context passed to the AsyncTask when the system instantiates it. private final Context localContext; final CustomHTTPClient HTTPClient; - final ServiceParser weatherService; + final ServiceCurrentParser weatherService; public CurrentTask(final Context context, final CustomHTTPClient HTTPClient, - final ServiceParser weatherService) { + final ServiceCurrentParser weatherService) { this.localContext = context; this.HTTPClient = HTTPClient; this.weatherService = weatherService; diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java index 382ce2b..4819a4e 100644 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/fragment/overview/OverviewFragment.java @@ -40,10 +40,10 @@ import name.gumartinm.weather.information.httpclient.CustomHTTPClient; import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; import name.gumartinm.weather.information.model.forecastweather.Forecast; -import name.gumartinm.weather.information.parser.JPOSWeatherParser; +import name.gumartinm.weather.information.parser.JPOSForecastParser; import name.gumartinm.weather.information.service.IconsList; import name.gumartinm.weather.information.service.PermanentStorage; -import name.gumartinm.weather.information.service.ServiceParser; +import name.gumartinm.weather.information.service.ServiceForecastParser; public class OverviewFragment extends ListFragment { private static final String TAG = "OverviewFragment"; @@ -147,7 +147,7 @@ public class OverviewFragment extends ListFragment { final OverviewTask task = new OverviewTask( this.getActivity().getApplicationContext(), new CustomHTTPClient(AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")), - new ServiceParser(new JPOSWeatherParser())); + new ServiceForecastParser(new JPOSForecastParser())); task.execute(weatherLocation.getLatitude(), weatherLocation.getLongitude()); } @@ -337,10 +337,10 @@ public class OverviewFragment extends ListFragment { // Store the context passed to the AsyncTask when the system instantiates it. private final Context localContext; private final CustomHTTPClient HTTPClient; - private final ServiceParser weatherService; + private final ServiceForecastParser weatherService; public OverviewTask(final Context context, final CustomHTTPClient HTTPClient, - final ServiceParser weatherService) { + final ServiceForecastParser weatherService) { this.localContext = context; this.HTTPClient = HTTPClient; this.weatherService = weatherService; diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java index 3b9dcd7..a0f4c75 100644 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/notification/NotificationIntentService.java @@ -32,9 +32,9 @@ import name.gumartinm.weather.information.httpclient.CustomHTTPClient; import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; import name.gumartinm.weather.information.model.currentweather.Current; -import name.gumartinm.weather.information.parser.JPOSWeatherParser; +import name.gumartinm.weather.information.parser.JPOSCurrentParser; import name.gumartinm.weather.information.service.IconsList; -import name.gumartinm.weather.information.service.ServiceParser; +import name.gumartinm.weather.information.service.ServiceCurrentParser; public class NotificationIntentService extends IntentService { @@ -51,7 +51,7 @@ public class NotificationIntentService extends IntentService { final WeatherLocation weatherLocation = query.queryDataBase(); if (weatherLocation != null) { - final ServiceParser weatherService = new ServiceParser(new JPOSWeatherParser()); + final ServiceCurrentParser weatherService = new ServiceCurrentParser(new JPOSCurrentParser()); final CustomHTTPClient HTTPClient = new CustomHTTPClient( AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")); @@ -81,7 +81,7 @@ public class NotificationIntentService extends IntentService { } private Current doInBackgroundThrowable(final WeatherLocation weatherLocation, - final CustomHTTPClient HTTPClient, final ServiceParser weatherService) + final CustomHTTPClient HTTPClient, final ServiceCurrentParser weatherService) throws ClientProtocolException, MalformedURLException, URISyntaxException, JsonParseException, IOException { diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/IJPOSParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/IJPOSParser.java deleted file mode 100644 index bdd501f..0000000 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/IJPOSParser.java +++ /dev/null @@ -1,17 +0,0 @@ -package name.gumartinm.weather.information.parser; - -import com.fasterxml.jackson.core.JsonParseException; -import name.gumartinm.weather.information.model.currentweather.Current; -import name.gumartinm.weather.information.model.forecastweather.Forecast; - -import java.io.IOException; - - -public interface IJPOSParser { - - public Current retrieveCurrenFromJPOS(final String jsonData) - throws JsonParseException, IOException; - - public Forecast retrieveForecastFromJPOS(final String jsonData) - throws JsonParseException, IOException; -} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSCurrentParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSCurrentParser.java new file mode 100644 index 0000000..473ad72 --- /dev/null +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSCurrentParser.java @@ -0,0 +1,207 @@ +package name.gumartinm.weather.information.parser; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +import java.io.IOException; +import java.util.ArrayList; + +import name.gumartinm.weather.information.model.currentweather.Clouds; +import name.gumartinm.weather.information.model.currentweather.Coord; +import name.gumartinm.weather.information.model.currentweather.Current; +import name.gumartinm.weather.information.model.currentweather.Main; +import name.gumartinm.weather.information.model.currentweather.Rain; +import name.gumartinm.weather.information.model.currentweather.Snow; +import name.gumartinm.weather.information.model.currentweather.Sys; +import name.gumartinm.weather.information.model.currentweather.Weather; +import name.gumartinm.weather.information.model.currentweather.Wind; + +public class JPOSCurrentParser { + + public Current retrieveCurrenFromJPOS(final String jsonData) + throws JsonParseException, IOException { + final JsonFactory f = new JsonFactory(); + + final Current currentWeatherData = new Current(); + currentWeatherData.setClouds(new Clouds()); + currentWeatherData.setCoord(new Coord()); + currentWeatherData.setMain(new Main()); + currentWeatherData.setRain(new Rain()); + currentWeatherData.setSys(new Sys()); + currentWeatherData.setSnow(new Snow()); + currentWeatherData + .setWeather(new ArrayList()); + currentWeatherData.setWind(new Wind()); + final JsonParser jParser = f.createParser(jsonData); + + this.getCurrentWeatherData(currentWeatherData, jParser); + + return currentWeatherData; + } + + private void getCurrentWeatherData(final Current currentWeatherData, + final JsonParser jParser) throws JsonParseException, IOException { + if (jParser.nextToken() == JsonToken.START_OBJECT) { + + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String fieldname = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); + if (nextToken == JsonToken.START_OBJECT) { + this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); + } + if (nextToken == JsonToken.START_ARRAY) { + JsonToken tokenNext = jParser.nextToken(); + while (tokenNext != JsonToken.END_ARRAY) { + if (tokenNext == JsonToken.START_OBJECT) { + this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); + } + tokenNext = jParser.nextToken(); + } + } + if ((nextToken == JsonToken.VALUE_NUMBER_INT) + || (nextToken == JsonToken.VALUE_STRING)) { + this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); + } + } + } + } + + private void getCurrentWeatherDataObjects(final Current currentWeatherData, + final JsonParser jParser, final String fieldname) throws JsonParseException, + IOException { + if ("coord".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("lon".equals(namefield)) { + currentWeatherData.getCoord().setLon(jParser.getDoubleValue()); + } + if ("lat".equals(namefield)) { + currentWeatherData.getCoord().setLat(jParser.getDoubleValue()); + } + } + } + if ("sys".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("message".equals(namefield)) { + currentWeatherData.getSys().setMessage(jParser.getDoubleValue()); + } + if ("country".equals(namefield)) { + currentWeatherData.getSys().setCountry(jParser.getValueAsString()); + } + if ("sunrise".equals(namefield)) { + currentWeatherData.getSys().setSunrise(jParser.getValueAsLong()); + } + if ("sunset".equals(namefield)) { + currentWeatherData.getSys().setSunset(jParser.getValueAsLong()); + } + } + } + if ("weather".equals(fieldname)) { + final Weather weather = new Weather(); + currentWeatherData.getWeather().add(weather); + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("id".equals(namefield)) { + weather.setId(jParser.getIntValue()); + } + if ("main".equals(namefield)) { + weather.setMain(jParser.getText()); + } + if ("description".equals(namefield)) { + weather.setDescription(jParser.getText()); + } + if ("icon".equals(namefield)) { + weather.setIcon(jParser.getText()); + } + + } + } + if ("base".equals(fieldname)) { + currentWeatherData.setBase(jParser.getText()); + } + if ("main".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("temp".equals(namefield)) { + currentWeatherData.getMain().setTemp(jParser.getDoubleValue()); + } + if ("temp_min".equals(namefield)) { + currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue()); + } + if ("temp_max".equals(namefield)) { + currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue()); + } + if ("pressure".equals(namefield)) { + currentWeatherData.getMain().setPressure(jParser.getDoubleValue()); + } + if ("sea_level".equals(namefield)) { + currentWeatherData.getMain().setSea_level(jParser.getDoubleValue()); + } + if ("grnd_level".equals(namefield)) { + currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue()); + } + if ("humidity".equals(namefield)) { + currentWeatherData.getMain().setHumidity(jParser.getDoubleValue()); + } + } + } + if ("wind".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("speed".equals(namefield)) { + currentWeatherData.getWind().setSpeed(jParser.getDoubleValue()); + } + if ("deg".equals(namefield)) { + currentWeatherData.getWind().setDeg(jParser.getDoubleValue()); + } + } + } + if ("clouds".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("all".equals(namefield)) { + currentWeatherData.getClouds().setAll(jParser.getDoubleValue()); + } + } + } + if ("dt".equals(fieldname)) { + currentWeatherData.setDt(jParser.getLongValue()); + } + if ("rain".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("3h".equals(namefield)) { + currentWeatherData.getRain().set3h(jParser.getDoubleValue()); + } + } + } + if ("snow".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("3h".equals(namefield)) { + currentWeatherData.getSnow().set3h(jParser.getDoubleValue()); + } + } + } + if ("id".equals(fieldname)) { + currentWeatherData.setId(jParser.getLongValue()); + } + if ("name".equals(fieldname)) { + currentWeatherData.setName(jParser.getText()); + } + if ("cod".equals(fieldname)) { + currentWeatherData.setCod(jParser.getIntValue()); + } + } +} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSForecastParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSForecastParser.java new file mode 100644 index 0000000..7dd4d46 --- /dev/null +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSForecastParser.java @@ -0,0 +1,213 @@ +package name.gumartinm.weather.information.parser; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonToken; + +import java.io.IOException; +import java.util.ArrayList; + +import name.gumartinm.weather.information.model.forecastweather.City; +import name.gumartinm.weather.information.model.forecastweather.Coord; +import name.gumartinm.weather.information.model.forecastweather.Forecast; +import name.gumartinm.weather.information.model.forecastweather.List; +import name.gumartinm.weather.information.model.forecastweather.Temp; +import name.gumartinm.weather.information.model.forecastweather.Weather; + +public class JPOSForecastParser { + + public Forecast retrieveForecastFromJPOS(final String jsonData) + throws JsonParseException, IOException { + final JsonFactory f = new JsonFactory(); + + final Forecast forecastWeatherData = new Forecast(); + forecastWeatherData + .setList(new ArrayList(15)); + final City city = new City(); + city.setCoord(new Coord()); + forecastWeatherData.setCity(city); + final JsonParser jParser = f.createParser(jsonData); + + this.getForecastWeatherData(forecastWeatherData, jParser); + + return forecastWeatherData; + } + + private void getForecastWeatherData(final Forecast forecastWeatherData, + final JsonParser jParser) throws JsonParseException, IOException { + if (jParser.nextToken() == JsonToken.START_OBJECT) { + + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String fieldname = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); + if (nextToken == JsonToken.START_OBJECT) { + this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); + } + if (nextToken == JsonToken.START_ARRAY) { + JsonToken tokenNext = jParser.nextToken(); + while (tokenNext != JsonToken.END_ARRAY) { + if (tokenNext == JsonToken.START_OBJECT) { + this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); + } + tokenNext = jParser.nextToken(); + } + } + if ((nextToken == JsonToken.VALUE_NUMBER_INT) + || (nextToken == JsonToken.VALUE_STRING)) { + this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); + } + } + } + } + + private void getForecastWeatherDataObjects(final Forecast forecastWeatherData, + final JsonParser jParser, final String fieldname) throws JsonParseException, + IOException { + + if ("cod".equals(fieldname)) { + final String stringCod = jParser.getText(); + forecastWeatherData.setCod(Long.valueOf(stringCod)); + } + if ("message".equals(fieldname)) { + forecastWeatherData.setMessage(jParser.getDoubleValue()); + } + if ("city".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); // move to + // value + if ("id".equals(namefield)) { + forecastWeatherData.getCity().setId(jParser.getLongValue()); + } + if ("name".equals(namefield)) { + forecastWeatherData.getCity().setName(jParser.getText()); + } + if ("coord".equals(namefield)) { + if (nextToken == JsonToken.START_OBJECT) { + this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield); + } + } + if ("country".equals(namefield)) { + forecastWeatherData.getCity().setCountry(jParser.getText()); + } + if ("population".equals(namefield)) { + forecastWeatherData.getCity().setPopulation(jParser.getLongValue()); + } + } + } + if ("cnt".equals(fieldname)) { + forecastWeatherData.setCnt(jParser.getIntValue()); + } + if ("coord".equals(fieldname)) { + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("lon".equals(namefield)) { + forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue()); + } + if ("lat".equals(namefield)) { + forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue()); + } + } + } + if ("list".equals(fieldname)) { + final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List(); + list.setTemp(new Temp()); + list.setWeather(new ArrayList()); + forecastWeatherData.getList().add(list); + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + final JsonToken nextToken = jParser.nextToken(); // move to + // value + if ("dt".equals(namefield)) { + list.setDt(jParser.getLongValue()); + } + if ("temp".equals(namefield)) { + if (nextToken == JsonToken.START_OBJECT) { + this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield); + } + } + if ("pressure".equals(namefield)) { + list.setPressure(jParser.getDoubleValue()); + } + if ("humidity".equals(namefield)) { + list.setHumidity(jParser.getDoubleValue()); + } + if ("weather".equals(namefield)) { + if (nextToken == JsonToken.START_ARRAY) { + JsonToken tokenNext = jParser.nextToken(); + while (tokenNext != JsonToken.END_ARRAY) { + if (tokenNext == JsonToken.START_OBJECT) { + this.getForecastWeatherDataObjects(forecastWeatherData, jParser, + namefield); + } + tokenNext = jParser.nextToken(); + } + } + } + if ("speed".equals(namefield)) { + list.setSpeed(jParser.getDoubleValue()); + } + if ("deg".equals(namefield)) { + list.setDeg(jParser.getDoubleValue()); + } + if ("clouds".equals(namefield)) { + list.setClouds(jParser.getDoubleValue()); + } + if ("rain".equals(namefield)) { + list.setRain(jParser.getDoubleValue()); + } + } + } + if ("temp".equals(fieldname)) { + final name.gumartinm.weather.information.model.forecastweather.List list = + forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1)); + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + if ("day".equals(namefield)) { + list.getTemp().setDay(jParser.getDoubleValue()); + } + if ("min".equals(namefield)) { + list.getTemp().setMin(jParser.getDoubleValue()); + } + if ("max".equals(namefield)) { + list.getTemp().setMax(jParser.getDoubleValue()); + } + if ("night".equals(namefield)) { + list.getTemp().setNight(jParser.getDoubleValue()); + } + if ("eve".equals(namefield)) { + list.getTemp().setEve(jParser.getDoubleValue()); + } + if ("morn".equals(namefield)) { + list.getTemp().setMorn(jParser.getDoubleValue()); + } + } + } + if ("weather".equals(fieldname)) { + final name.gumartinm.weather.information.model.forecastweather.List list = + forecastWeatherData.getList().get((forecastWeatherData.getList().size() - 1)); + final Weather weather = new Weather(); + while (jParser.nextToken() != JsonToken.END_OBJECT) { + final String namefield = jParser.getCurrentName(); + jParser.nextToken(); // move to value + + if ("id".equals(namefield)) { + weather.setId(jParser.getIntValue()); + } + if ("main".equals(namefield)) { + weather.setMain(jParser.getText()); + } + if ("description".equals(namefield)) { + weather.setDescription(jParser.getText()); + } + if ("icon".equals(namefield)) { + weather.setIcon(jParser.getText()); + } + } + list.getWeather().add(weather); + } + } +} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSWeatherParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSWeatherParser.java deleted file mode 100644 index 176356c..0000000 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/parser/JPOSWeatherParser.java +++ /dev/null @@ -1,405 +0,0 @@ -package name.gumartinm.weather.information.parser; - -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import name.gumartinm.weather.information.model.currentweather.Clouds; -import name.gumartinm.weather.information.model.currentweather.Current; -import name.gumartinm.weather.information.model.currentweather.Main; -import name.gumartinm.weather.information.model.currentweather.Rain; -import name.gumartinm.weather.information.model.currentweather.Snow; -import name.gumartinm.weather.information.model.currentweather.Sys; -import name.gumartinm.weather.information.model.currentweather.Wind; -import name.gumartinm.weather.information.model.forecastweather.City; -import name.gumartinm.weather.information.model.forecastweather.Forecast; -import name.gumartinm.weather.information.model.forecastweather.Temp; - -import java.io.IOException; -import java.util.ArrayList; - -public class JPOSWeatherParser implements IJPOSParser { - - @Override - public Current retrieveCurrenFromJPOS(final String jsonData) - throws JsonParseException, IOException { - final JsonFactory f = new JsonFactory(); - - final Current currentWeatherData = new Current(); - currentWeatherData.setClouds(new Clouds()); - currentWeatherData.setCoord(new name.gumartinm.weather.information.model.currentweather.Coord()); - currentWeatherData.setMain(new Main()); - currentWeatherData.setRain(new Rain()); - currentWeatherData.setSys(new Sys()); - currentWeatherData.setSnow(new Snow()); - currentWeatherData - .setWeather(new ArrayList()); - currentWeatherData.setWind(new Wind()); - final JsonParser jParser = f.createParser(jsonData); - - this.getCurrentWeatherData(currentWeatherData, jParser); - - return currentWeatherData; - } - - @Override - public Forecast retrieveForecastFromJPOS(final String jsonData) - throws JsonParseException, IOException { - final JsonFactory f = new JsonFactory(); - - final Forecast forecastWeatherData = new Forecast(); - forecastWeatherData - .setList(new ArrayList(15)); - final City city = new City(); - city.setCoord(new name.gumartinm.weather.information.model.forecastweather.Coord()); - forecastWeatherData.setCity(city); - final JsonParser jParser = f.createParser(jsonData); - - this.getForecastWeatherData(forecastWeatherData, jParser); - - return forecastWeatherData; - } - - private void getCurrentWeatherData(final Current currentWeatherData, - final JsonParser jParser) throws JsonParseException, IOException { - if (jParser.nextToken() == JsonToken.START_OBJECT) { - - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String fieldname = jParser.getCurrentName(); - final JsonToken nextToken = jParser.nextToken(); - if (nextToken == JsonToken.START_OBJECT) { - this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); - } - if (nextToken == JsonToken.START_ARRAY) { - JsonToken tokenNext = jParser.nextToken(); - while (tokenNext != JsonToken.END_ARRAY) { - if (tokenNext == JsonToken.START_OBJECT) { - this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); - } - tokenNext = jParser.nextToken(); - } - } - if ((nextToken == JsonToken.VALUE_NUMBER_INT) - || (nextToken == JsonToken.VALUE_STRING)) { - this.getCurrentWeatherDataObjects(currentWeatherData, jParser, fieldname); - } - } - } - } - - private void getCurrentWeatherDataObjects(final Current currentWeatherData, - final JsonParser jParser, final String fieldname) throws JsonParseException, - IOException { - if ("coord".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("lon".equals(namefield)) { - currentWeatherData.getCoord().setLon(jParser.getDoubleValue()); - } - if ("lat".equals(namefield)) { - currentWeatherData.getCoord().setLat(jParser.getDoubleValue()); - } - } - } - if ("sys".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("message".equals(namefield)) { - currentWeatherData.getSys().setMessage(jParser.getDoubleValue()); - } - if ("country".equals(namefield)) { - currentWeatherData.getSys().setCountry(jParser.getValueAsString()); - } - if ("sunrise".equals(namefield)) { - currentWeatherData.getSys().setSunrise(jParser.getValueAsLong()); - } - if ("sunset".equals(namefield)) { - currentWeatherData.getSys().setSunset(jParser.getValueAsLong()); - } - } - } - if ("weather".equals(fieldname)) { - final name.gumartinm.weather.information.model.currentweather.Weather weather = new name.gumartinm.weather.information.model.currentweather.Weather(); - currentWeatherData.getWeather().add(weather); - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("id".equals(namefield)) { - weather.setId(jParser.getIntValue()); - } - if ("main".equals(namefield)) { - weather.setMain(jParser.getText()); - } - if ("description".equals(namefield)) { - weather.setDescription(jParser.getText()); - } - if ("icon".equals(namefield)) { - weather.setIcon(jParser.getText()); - } - - } - } - if ("base".equals(fieldname)) { - currentWeatherData.setBase(jParser.getText()); - } - if ("main".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("temp".equals(namefield)) { - currentWeatherData.getMain().setTemp(jParser.getDoubleValue()); - } - if ("temp_min".equals(namefield)) { - currentWeatherData.getMain().setTemp_min(jParser.getDoubleValue()); - } - if ("temp_max".equals(namefield)) { - currentWeatherData.getMain().setTemp_max(jParser.getDoubleValue()); - } - if ("pressure".equals(namefield)) { - currentWeatherData.getMain().setPressure(jParser.getDoubleValue()); - } - if ("sea_level".equals(namefield)) { - currentWeatherData.getMain().setSea_level(jParser.getDoubleValue()); - } - if ("grnd_level".equals(namefield)) { - currentWeatherData.getMain().setGrnd_level(jParser.getDoubleValue()); - } - if ("humidity".equals(namefield)) { - currentWeatherData.getMain().setHumidity(jParser.getDoubleValue()); - } - } - } - if ("wind".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("speed".equals(namefield)) { - currentWeatherData.getWind().setSpeed(jParser.getDoubleValue()); - } - if ("deg".equals(namefield)) { - currentWeatherData.getWind().setDeg(jParser.getDoubleValue()); - } - } - } - if ("clouds".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("all".equals(namefield)) { - currentWeatherData.getClouds().setAll(jParser.getDoubleValue()); - } - } - } - if ("dt".equals(fieldname)) { - currentWeatherData.setDt(jParser.getLongValue()); - } - if ("rain".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("3h".equals(namefield)) { - currentWeatherData.getRain().set3h(jParser.getDoubleValue()); - } - } - } - if ("snow".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("3h".equals(namefield)) { - currentWeatherData.getSnow().set3h(jParser.getDoubleValue()); - } - } - } - if ("id".equals(fieldname)) { - currentWeatherData.setId(jParser.getLongValue()); - } - if ("name".equals(fieldname)) { - currentWeatherData.setName(jParser.getText()); - } - if ("cod".equals(fieldname)) { - currentWeatherData.setCod(jParser.getIntValue()); - } - } - - private void getForecastWeatherData(final Forecast forecastWeatherData, - final JsonParser jParser) throws JsonParseException, IOException { - if (jParser.nextToken() == JsonToken.START_OBJECT) { - - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String fieldname = jParser.getCurrentName(); - final JsonToken nextToken = jParser.nextToken(); - if (nextToken == JsonToken.START_OBJECT) { - this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); - } - if (nextToken == JsonToken.START_ARRAY) { - JsonToken tokenNext = jParser.nextToken(); - while (tokenNext != JsonToken.END_ARRAY) { - if (tokenNext == JsonToken.START_OBJECT) { - this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); - } - tokenNext = jParser.nextToken(); - } - } - if ((nextToken == JsonToken.VALUE_NUMBER_INT) - || (nextToken == JsonToken.VALUE_STRING)) { - this.getForecastWeatherDataObjects(forecastWeatherData, jParser, fieldname); - } - } - } - } - - private void getForecastWeatherDataObjects(final Forecast forecastWeatherData, - final JsonParser jParser, final String fieldname) throws JsonParseException, - IOException { - - if ("cod".equals(fieldname)) { - final String stringCod = jParser.getText(); - forecastWeatherData.setCod(Long.valueOf(stringCod)); - } - if ("message".equals(fieldname)) { - forecastWeatherData.setMessage(jParser.getDoubleValue()); - } - if ("city".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - final JsonToken nextToken = jParser.nextToken(); // move to - // value - if ("id".equals(namefield)) { - forecastWeatherData.getCity().setId(jParser.getLongValue()); - } - if ("name".equals(namefield)) { - forecastWeatherData.getCity().setName(jParser.getText()); - } - if ("coord".equals(namefield)) { - if (nextToken == JsonToken.START_OBJECT) { - this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield); - } - } - if ("country".equals(namefield)) { - forecastWeatherData.getCity().setCountry(jParser.getText()); - } - if ("population".equals(namefield)) { - forecastWeatherData.getCity().setPopulation(jParser.getLongValue()); - } - } - } - if ("cnt".equals(fieldname)) { - forecastWeatherData.setCnt(jParser.getIntValue()); - } - if ("coord".equals(fieldname)) { - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("lon".equals(namefield)) { - forecastWeatherData.getCity().getCoord().setLon(jParser.getDoubleValue()); - } - if ("lat".equals(namefield)) { - forecastWeatherData.getCity().getCoord().setLat(jParser.getDoubleValue()); - } - } - } - if ("list".equals(fieldname)) { - final name.gumartinm.weather.information.model.forecastweather.List list = new name.gumartinm.weather.information.model.forecastweather.List(); - list.setTemp(new Temp()); - list.setWeather(new ArrayList()); - forecastWeatherData.getList().add(list); - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - final JsonToken nextToken = jParser.nextToken(); // move to - // value - if ("dt".equals(namefield)) { - list.setDt(jParser.getLongValue()); - } - if ("temp".equals(namefield)) { - if (nextToken == JsonToken.START_OBJECT) { - this.getForecastWeatherDataObjects(forecastWeatherData, jParser, namefield); - } - } - if ("pressure".equals(namefield)) { - list.setPressure(jParser.getDoubleValue()); - } - if ("humidity".equals(namefield)) { - list.setHumidity(jParser.getDoubleValue()); - } - if ("weather".equals(namefield)) { - if (nextToken == JsonToken.START_ARRAY) { - JsonToken tokenNext = jParser.nextToken(); - while (tokenNext != JsonToken.END_ARRAY) { - if (tokenNext == JsonToken.START_OBJECT) { - this.getForecastWeatherDataObjects(forecastWeatherData, jParser, - namefield); - } - tokenNext = jParser.nextToken(); - } - } - } - if ("speed".equals(namefield)) { - list.setSpeed(jParser.getDoubleValue()); - } - if ("deg".equals(namefield)) { - list.setDeg(jParser.getDoubleValue()); - } - if ("clouds".equals(namefield)) { - list.setClouds(jParser.getDoubleValue()); - } - if ("rain".equals(namefield)) { - list.setRain(jParser.getDoubleValue()); - } - } - } - if ("temp".equals(fieldname)) { - final name.gumartinm.weather.information.model.forecastweather.List list = forecastWeatherData - .getList().get( - (forecastWeatherData.getList().size() - 1)); - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - if ("day".equals(namefield)) { - list.getTemp().setDay(jParser.getDoubleValue()); - } - if ("min".equals(namefield)) { - list.getTemp().setMin(jParser.getDoubleValue()); - } - if ("max".equals(namefield)) { - list.getTemp().setMax(jParser.getDoubleValue()); - } - if ("night".equals(namefield)) { - list.getTemp().setNight(jParser.getDoubleValue()); - } - if ("eve".equals(namefield)) { - list.getTemp().setEve(jParser.getDoubleValue()); - } - if ("morn".equals(namefield)) { - list.getTemp().setMorn(jParser.getDoubleValue()); - } - } - } - if ("weather".equals(fieldname)) { - final name.gumartinm.weather.information.model.forecastweather.List list = forecastWeatherData - .getList().get( - (forecastWeatherData.getList().size() - 1)); - final name.gumartinm.weather.information.model.forecastweather.Weather weather = new name.gumartinm.weather.information.model.forecastweather.Weather(); - while (jParser.nextToken() != JsonToken.END_OBJECT) { - final String namefield = jParser.getCurrentName(); - jParser.nextToken(); // move to value - - if ("id".equals(namefield)) { - weather.setId(jParser.getIntValue()); - } - if ("main".equals(namefield)) { - weather.setMain(jParser.getText()); - } - if ("description".equals(namefield)) { - weather.setDescription(jParser.getText()); - } - if ("icon".equals(namefield)) { - weather.setIcon(jParser.getText()); - } - } - list.getWeather().add(weather); - } - } -} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceCurrentParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceCurrentParser.java new file mode 100644 index 0000000..c7b727f --- /dev/null +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceCurrentParser.java @@ -0,0 +1,35 @@ +package name.gumartinm.weather.information.service; + +import com.fasterxml.jackson.core.JsonParseException; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.Locale; + +import name.gumartinm.weather.information.model.currentweather.Current; +import name.gumartinm.weather.information.parser.JPOSCurrentParser; + +public class ServiceCurrentParser { + private final JPOSCurrentParser JPOSParser; + + public ServiceCurrentParser(final JPOSCurrentParser jposParser) { + this.JPOSParser = jposParser; + } + + public Current retrieveCurrentFromJPOS(final String jsonData) + throws JsonParseException, IOException { + return this.JPOSParser.retrieveCurrenFromJPOS(jsonData); + } + + public String createURIAPICurrent(final String urlAPI, final String APIVersion, + final double latitude, final double longitude) { + + final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US); + final Object[] values = new Object[3]; + values[0] = APIVersion; + values[1] = latitude; + values[2] = longitude; + + return formatURIAPI.format(values); + } +} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceForecastParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceForecastParser.java new file mode 100644 index 0000000..eac2dab --- /dev/null +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceForecastParser.java @@ -0,0 +1,36 @@ +package name.gumartinm.weather.information.service; + +import com.fasterxml.jackson.core.JsonParseException; + +import java.io.IOException; +import java.text.MessageFormat; +import java.util.Locale; + +import name.gumartinm.weather.information.model.forecastweather.Forecast; +import name.gumartinm.weather.information.parser.JPOSForecastParser; + +public class ServiceForecastParser { + private final JPOSForecastParser JPOSParser; + + public ServiceForecastParser(final JPOSForecastParser jposParser) { + this.JPOSParser = jposParser; + } + + public Forecast retrieveForecastFromJPOS(final String jsonData) + throws JsonParseException, IOException { + return this.JPOSParser.retrieveForecastFromJPOS(jsonData); + } + + public String createURIAPIForecast(final String urlAPI, final String APIVersion, + final double latitude, final double longitude, final String resultsNumber) { + + final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US); + final Object[] values = new Object[4]; + values[0] = APIVersion; + values[1] = latitude; + values[2] = longitude; + values[3] = resultsNumber; + + return formatURIAPI.format(values); + } +} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceParser.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceParser.java deleted file mode 100644 index 80534a1..0000000 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/service/ServiceParser.java +++ /dev/null @@ -1,64 +0,0 @@ -package name.gumartinm.weather.information.service; - -import com.fasterxml.jackson.core.JsonParseException; -import name.gumartinm.weather.information.model.currentweather.Current; -import name.gumartinm.weather.information.model.forecastweather.Forecast; -import name.gumartinm.weather.information.parser.IJPOSParser; - -import java.io.IOException; -import java.text.MessageFormat; -import java.util.Locale; - - -public class ServiceParser { - private final IJPOSParser JPOSParser; - - public ServiceParser(final IJPOSParser JPOSWeatherParser) { - this.JPOSParser = JPOSWeatherParser; - } - - public Current retrieveCurrentFromJPOS(final String jsonData) - throws JsonParseException, IOException { - return this.JPOSParser.retrieveCurrenFromJPOS(jsonData); - } - - public Forecast retrieveForecastFromJPOS(final String jsonData) - throws JsonParseException, IOException { - return this.JPOSParser.retrieveForecastFromJPOS(jsonData); - } - - public String createURIAPIForecast(final String urlAPI, final String APIVersion, - final double latitude, final double longitude, final String resultsNumber) { - - final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US); - final Object[] values = new Object[4]; - values[0] = APIVersion; - values[1] = latitude; - values[2] = longitude; - values[3] = resultsNumber; - - return formatURIAPI.format(values); - } - - public String createURIAPICurrent(final String urlAPI, final String APIVersion, - final double latitude, final double longitude) { - - final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US); - final Object[] values = new Object[3]; - values[0] = APIVersion; - values[1] = latitude; - values[2] = longitude; - - return formatURIAPI.format(values); - } - - public String createURIAPIicon(final String icon, final String urlAPI) { - - final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.US); - final Object[] values = new Object[1]; - values[0] = icon; - - return formatURIAPI.format(values); - } - -} diff --git a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/widget/service/WidgetIntentService.java b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/widget/service/WidgetIntentService.java index 826e945..61dbb9c 100644 --- a/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/widget/service/WidgetIntentService.java +++ b/Android/WeatherInformation/app/src/main/java/name/gumartinm/weather/information/widget/service/WidgetIntentService.java @@ -20,10 +20,10 @@ import name.gumartinm.weather.information.httpclient.CustomHTTPClient; import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; import name.gumartinm.weather.information.model.currentweather.Current; -import name.gumartinm.weather.information.parser.JPOSWeatherParser; +import name.gumartinm.weather.information.parser.JPOSCurrentParser; import name.gumartinm.weather.information.service.IconsList; import name.gumartinm.weather.information.service.PermanentStorage; -import name.gumartinm.weather.information.service.ServiceParser; +import name.gumartinm.weather.information.service.ServiceCurrentParser; import name.gumartinm.weather.information.widget.WidgetConfigure; import org.apache.http.client.ClientProtocolException; @@ -107,7 +107,7 @@ public class WidgetIntentService extends IntentService { private Current getRemoteCurrent(final WeatherLocation weatherLocation) { - final ServiceParser weatherService = new ServiceParser(new JPOSWeatherParser()); + final ServiceCurrentParser weatherService = new ServiceCurrentParser(new JPOSCurrentParser()); final CustomHTTPClient HTTPClient = new CustomHTTPClient( AndroidHttpClient.newInstance("Android 4.3 WeatherInformation Agent")); @@ -133,7 +133,7 @@ public class WidgetIntentService extends IntentService { } private Current getRemoteCurrentThrowable(final WeatherLocation weatherLocation, - final CustomHTTPClient HTTPClient, final ServiceParser weatherService) + final CustomHTTPClient HTTPClient, final ServiceCurrentParser weatherService) throws ClientProtocolException, MalformedURLException, URISyntaxException, JsonParseException, IOException { diff --git a/Android/WeatherInformation/app/src/main/res/raw/licenses.html b/Android/WeatherInformation/app/src/main/res/raw/licenses.html index a668e4b..045f8dc 100644 --- a/Android/WeatherInformation/app/src/main/res/raw/licenses.html +++ b/Android/WeatherInformation/app/src/main/res/raw/licenses.html @@ -45,6 +45,7 @@ Please link to http://www.gnome.org where available.
    +
  • Consts.java
  • ContentType.java
http://hc.apache.org/httpclient-3.x/license.html -- 2.1.4