From e2572fc062714b6beb41dffb3701c7dd2f2acff2 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Sun, 30 Mar 2014 19:29:45 +0200 Subject: [PATCH] Using my Galaxy Nexus Android version --- AndroidManifest.xml | 7 +- project.properties | 2 +- res/layout/fragment_main.xml | 6 +- res/values/strings.xml | 4 +- .../exampletdd/WeatherInformationActivity.java | 70 ++------- .../activityinterface/OnClickButtons.java | 9 ++ .../exampletdd/fragment/WeatherDataFragment.java | 172 ++++++++++++--------- .../exampletdd/httpclient/WeatherHTTPClient.java | 9 +- .../exampletdd/parser/JPOSWeatherParser.java | 30 +++- .../example/exampletdd/service/WeatherService.java | 4 +- tests/AndroidManifest.xml | 2 +- tests/project.properties | 2 +- 12 files changed, 163 insertions(+), 154 deletions(-) create mode 100644 src/de/example/exampletdd/activityinterface/OnClickButtons.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 155fbb4..ba00203 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -2,11 +2,12 @@ + android:versionName="1.0" android:installLocation="auto"> + android:minSdkVersion="18" + android:targetSdkVersion="18" android:maxSdkVersion="18"/> + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 1e684ca..56a3a09 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -6,9 +6,9 @@ Get weather http://api.openweathermap.org/data/{0}/weather?lat={1}&lon={2} http://api.openweathermap.org/data/{0}/weather?q={1} - http://openweathermap.org/img/w/{0} + http://openweathermap.org/img/w/{0}.png 2.5 - City,country + London,uk City name Latitude Longitude diff --git a/src/de/example/exampletdd/WeatherInformationActivity.java b/src/de/example/exampletdd/WeatherInformationActivity.java index cb4e1b3..c721b7d 100644 --- a/src/de/example/exampletdd/WeatherInformationActivity.java +++ b/src/de/example/exampletdd/WeatherInformationActivity.java @@ -1,49 +1,32 @@ package de.example.exampletdd; -import java.text.SimpleDateFormat; -import java.util.Date; - import android.app.Activity; import android.app.DialogFragment; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; -import android.widget.EditText; -import android.widget.ImageView; +import android.view.View; import de.example.exampletdd.activityinterface.ErrorMessage; -import de.example.exampletdd.activityinterface.UpdateWeatherData; +import de.example.exampletdd.activityinterface.OnClickButtons; import de.example.exampletdd.fragment.ErrorDialogFragment; import de.example.exampletdd.fragment.WeatherDataFragment; -import de.example.exampletdd.model.WeatherData; -public class WeatherInformationActivity extends Activity implements ErrorMessage, UpdateWeatherData { - EditText weatherDescription; - EditText temperature; - EditText maxTemperature; - EditText minTemperature; - EditText sunRise; - EditText sunSet; - ImageView imageIcon; +public class WeatherInformationActivity extends Activity implements ErrorMessage { + private OnClickButtons onclickButtons; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); + final WeatherDataFragment weatherDataFragment = new WeatherDataFragment(); + if (savedInstanceState == null) { this.getFragmentManager().beginTransaction() - .add(R.id.container, new WeatherDataFragment()).commit(); + .add(R.id.container, weatherDataFragment).commit(); } - this.weatherDescription = (EditText) this.findViewById(R.id.editTextWeatherDescription); - this.temperature = (EditText) this.findViewById(R.id.editTextTemperature); - this.maxTemperature = (EditText) this.findViewById(R.id.editTextMaxTemperature); - this.minTemperature = (EditText) this.findViewById(R.id.editTextMinTemperature); - this.sunRise = (EditText) this.findViewById(R.id.editTextSunRise); - this.sunSet = (EditText) this.findViewById(R.id.editTextSunSet); - this.imageIcon = (ImageView) this.findViewById(R.id.imageIcon); + this.onclickButtons = weatherDataFragment; } @Override @@ -73,40 +56,7 @@ public class WeatherInformationActivity extends Activity implements ErrorMessage newFragment.show(this.getFragmentManager(), "errorDialog"); } - - @Override - public void updateWeatherData(final WeatherData weatherData) { - - if (weatherData.getWeather() != null) { - this.weatherDescription.setText(weatherData.getWeather().getDescription()); - double conversion = weatherData.getMain().getTemp(); - conversion = conversion - 273.15; - this.temperature.setText(String.valueOf(conversion)); - conversion = weatherData.getMain().getMaxTemp(); - conversion = conversion - 273.15; - this.maxTemperature.setText(String.valueOf(conversion)); - conversion = weatherData.getMain().getMinTemp(); - conversion = conversion - 273.15; - this.minTemperature.setText(String.valueOf(conversion)); - } - - if (weatherData.getSystem() != null) { - long unixTime = weatherData.getSystem().getSunRiseTime(); - Date dateUnix = new Date(unixTime); - String dateFormatUnix = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z").format(dateUnix); - this.sunRise.setText(dateFormatUnix); - - unixTime = weatherData.getSystem().getSunSetTime(); - dateUnix = new Date(unixTime); - dateFormatUnix = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z").format(dateUnix); - this.sunSet.setText(dateFormatUnix); - } - - if (weatherData.getIconData() != null) { - final Bitmap icon = BitmapFactory.decodeByteArray( - weatherData.getIconData(), 0, - weatherData.getIconData().length); - this.imageIcon.setImageBitmap(icon); - } + public void onClickGetWeather(final View v) { + this.onclickButtons.onClickGetWeather(v); } } diff --git a/src/de/example/exampletdd/activityinterface/OnClickButtons.java b/src/de/example/exampletdd/activityinterface/OnClickButtons.java new file mode 100644 index 0000000..0400afe --- /dev/null +++ b/src/de/example/exampletdd/activityinterface/OnClickButtons.java @@ -0,0 +1,9 @@ +package de.example.exampletdd.activityinterface; + +import android.view.View; + +public interface OnClickButtons { + + public void onClickGetWeather(final View v); + +} diff --git a/src/de/example/exampletdd/fragment/WeatherDataFragment.java b/src/de/example/exampletdd/fragment/WeatherDataFragment.java index a8fcbae..1efe3fb 100644 --- a/src/de/example/exampletdd/fragment/WeatherDataFragment.java +++ b/src/de/example/exampletdd/fragment/WeatherDataFragment.java @@ -4,12 +4,17 @@ import java.io.IOException; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.text.DecimalFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import android.app.Activity; import android.app.Fragment; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.http.AndroidHttpClient; import android.os.AsyncTask; import android.os.Bundle; @@ -18,30 +23,47 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; +import android.widget.ImageView; import de.example.exampletdd.R; import de.example.exampletdd.activityinterface.ErrorMessage; -import de.example.exampletdd.activityinterface.UpdateWeatherData; +import de.example.exampletdd.activityinterface.OnClickButtons; import de.example.exampletdd.httpclient.WeatherHTTPClient; import de.example.exampletdd.model.WeatherData; import de.example.exampletdd.parser.IJPOSWeatherParser; import de.example.exampletdd.parser.JPOSWeatherParser; import de.example.exampletdd.service.WeatherService; -public class WeatherDataFragment extends Fragment { - private final Activity currentActivity; +public class WeatherDataFragment extends Fragment implements OnClickButtons { + private Activity currentActivity; + EditText weatherDescription; + EditText temperature; + EditText maxTemperature; + EditText minTemperature; + EditText sunRise; + EditText sunSet; + ImageView imageIcon; - public WeatherDataFragment() { - this.currentActivity = this.getActivity(); - } @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.fragment_main, container, false); + + this.currentActivity = this.getActivity(); + + this.weatherDescription = (EditText) rootView.findViewById(R.id.editTextWeatherDescription); + this.temperature = (EditText) rootView.findViewById(R.id.editTextTemperature); + this.maxTemperature = (EditText) rootView.findViewById(R.id.editTextMaxTemperature); + this.minTemperature = (EditText) rootView.findViewById(R.id.editTextMinTemperature); + this.sunRise = (EditText) rootView.findViewById(R.id.editTextSunRise); + this.sunSet = (EditText) rootView.findViewById(R.id.editTextSunSet); + this.imageIcon = (ImageView) rootView.findViewById(R.id.imageIcon); + return rootView; } + @Override public void onClickGetWeather(final View v) { final IJPOSWeatherParser JPOSWeatherParser = new JPOSWeatherParser(); @@ -60,6 +82,44 @@ public class WeatherDataFragment extends Fragment { weatherTask.execute(cityCountry.getText().toString()); } + public void updateWeatherData(final WeatherData weatherData) { + final DecimalFormat tempFormatter = new DecimalFormat("#####.#####"); + final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss Z"); + + if (weatherData.getWeather() != null) { + this.weatherDescription.setText(weatherData.getWeather() + .getDescription()); + double conversion = weatherData.getMain().getTemp(); + conversion = conversion - 273.15; + this.temperature.setText(tempFormatter.format(conversion)); + conversion = weatherData.getMain().getMaxTemp(); + conversion = conversion - 273.15; + this.maxTemperature.setText(tempFormatter.format(conversion)); + conversion = weatherData.getMain().getMinTemp(); + conversion = conversion - 273.15; + this.minTemperature.setText(tempFormatter.format(conversion)); + } + + if (weatherData.getSystem() != null) { + long unixTime = weatherData.getSystem().getSunRiseTime(); + Date unixDate = new Date(unixTime * 1000L); + String dateFormatUnix = dateFormat.format(unixDate); + this.sunRise.setText(dateFormatUnix); + + unixTime = weatherData.getSystem().getSunSetTime(); + unixDate = new Date(unixTime * 1000L); + dateFormatUnix = dateFormat.format(unixDate); + this.sunSet.setText(dateFormatUnix); + } + + if (weatherData.getIconData() != null) { + final Bitmap icon = BitmapFactory.decodeByteArray( + weatherData.getIconData(), 0, + weatherData.getIconData().length); + this.imageIcon.setImageBitmap(icon); + } + } + public class WeatherTask extends AsyncTask { private static final String TAG = "JSONWeatherTask"; private final WeatherHTTPClient weatherHTTPClient; @@ -73,91 +133,31 @@ public class WeatherDataFragment extends Fragment { @Override protected WeatherData doInBackground(final Object... params) { - final String cityCountry = (String) params[0]; - - final String urlAPICity = WeatherDataFragment.this.getResources() - .getString(R.string.uri_api_city); - final String APIVersion = WeatherDataFragment.this.getResources() - .getString(R.string.api_version); - - String url = this.weatherService.createURIAPICityCountry( - cityCountry, urlAPICity, APIVersion); + WeatherData weatherData = null; - String jsonData = null; try { - jsonData = this.weatherHTTPClient - .retrieveJSONDataFromAPI(new URL(url)); + weatherData = this.doInBackgroundThrowable(params); } catch (final ClientProtocolException e) { Log.e(TAG, "WeatherHTTPClient exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); } catch (final MalformedURLException e) { Log.e(TAG, "Syntax URL exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); } catch (final URISyntaxException e) { Log.e(TAG, "WeatherHTTPClient exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); } catch (final IOException e) { Log.e(TAG, "WeatherHTTPClient exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); + } catch (final JSONException e) { + Log.e(TAG, "WeatherService exception: ", e); } finally { this.weatherHTTPClient.close(); } - WeatherData weatherData = null; - if (jsonData != null) { - try { - weatherData = this.weatherService.retrieveWeather(jsonData); - } catch (final JSONException e) { - Log.e(TAG, "WeatherService exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); - } - - } - - if (weatherData != null) { - final String icon = weatherData.getWeather().getIcon(); - final String urlAPIicon = WeatherDataFragment.this - .getResources().getString(R.string.uri_api_icon); - - url = this.weatherService.createURIAPIicon(icon, urlAPIicon); - try { - final byte[] iconData = this.weatherHTTPClient - .retrieveDataFromAPI(new URL(url)).toByteArray(); - weatherData.setIconData(iconData); - } catch (final ClientProtocolException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); - } catch (final MalformedURLException e) { - Log.e(TAG, "Syntax URL exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); - } catch (final URISyntaxException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); - } catch (final IOException e) { - Log.e(TAG, "WeatherHTTPClient exception: ", e); - ((ErrorMessage) WeatherDataFragment.this.currentActivity) - .createErrorDialog(R.string.error_dialog_generic_error); - } finally { - this.weatherHTTPClient.close(); - } - } - return weatherData; } @Override protected void onPostExecute(final WeatherData weatherData) { if (weatherData != null) { - ((UpdateWeatherData) WeatherDataFragment.this.currentActivity) - .updateWeatherData(weatherData); + WeatherDataFragment.this.updateWeatherData(weatherData); } else { ((ErrorMessage) WeatherDataFragment.this.currentActivity) .createErrorDialog(R.string.error_dialog_generic_error); @@ -174,5 +174,35 @@ public class WeatherDataFragment extends Fragment { this.weatherHTTPClient.close(); } + + private WeatherData doInBackgroundThrowable(final Object... params) + throws ClientProtocolException, MalformedURLException, + URISyntaxException, IOException, JSONException { + final String cityCountry = (String) params[0]; + final String urlAPICity = WeatherDataFragment.this.getResources() + .getString(R.string.uri_api_city); + final String APIVersion = WeatherDataFragment.this.getResources() + .getString(R.string.api_version); + String url = this.weatherService.createURIAPICityCountry( + cityCountry, urlAPICity, APIVersion); + + + final String jsonData = this.weatherHTTPClient.retrieveJSONDataFromAPI(new URL(url)); + + + final WeatherData weatherData = this.weatherService.retrieveWeather(jsonData); + + + final String icon = weatherData.getWeather().getIcon(); + final String urlAPIicon = WeatherDataFragment.this + .getResources().getString(R.string.uri_api_icon); + url = this.weatherService.createURIAPIicon(icon, urlAPIicon); + final byte[] iconData = this.weatherHTTPClient + .retrieveDataFromAPI(new URL(url)).toByteArray(); + weatherData.setIconData(iconData); + + + return weatherData; + } } } diff --git a/src/de/example/exampletdd/httpclient/WeatherHTTPClient.java b/src/de/example/exampletdd/httpclient/WeatherHTTPClient.java index aab1745..de36985 100644 --- a/src/de/example/exampletdd/httpclient/WeatherHTTPClient.java +++ b/src/de/example/exampletdd/httpclient/WeatherHTTPClient.java @@ -35,12 +35,13 @@ public class WeatherHTTPClient { if (response != null) { final HttpEntity entity = response.getEntity(); if (entity != null) { - final String contentEncoding = entity - .getContentEncoding() - .getValue(); + // Not receiving encoding :( + // final String contentEncoding = entity + // .getContentEncoding() + // .getValue(); final ByteArrayOutputStream buffer = WeatherHTTPClient.this .sortResponse(response); - return new String(buffer.toByteArray(), contentEncoding); + return new String(buffer.toByteArray(), "UTF-8"); } throw new IOException("There is no entity"); diff --git a/src/de/example/exampletdd/parser/JPOSWeatherParser.java b/src/de/example/exampletdd/parser/JPOSWeatherParser.java index 98de7be..ab23011 100644 --- a/src/de/example/exampletdd/parser/JPOSWeatherParser.java +++ b/src/de/example/exampletdd/parser/JPOSWeatherParser.java @@ -1,5 +1,6 @@ package de.example.exampletdd.parser; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -25,7 +26,10 @@ public class JPOSWeatherParser implements IJPOSWeatherParser { final WeatherData.System system = new WeatherData.System(country, sunRiseTime, sunSetTime, message); - jsonObject = jsonWeatherData.getJSONObject("weather"); + // TODO: array of WeatherData.Weather :( + final JSONArray jsonArray = jsonWeatherData.getJSONArray("weather"); + jsonObject = jsonArray.getJSONObject(0); + final int id = jsonObject.getInt("id"); final String mainWeather = jsonObject.getString("main"); final String description = jsonObject.getString("description"); @@ -35,8 +39,8 @@ public class JPOSWeatherParser implements IJPOSWeatherParser { jsonObject = jsonWeatherData.getJSONObject("main"); final double temp = jsonObject.getDouble("temp"); - final double minTemp = jsonObject.getDouble("minTemp"); - final double maxTemp = jsonObject.getDouble("maxTemp"); + final double minTemp = jsonObject.getDouble("temp_min"); + final double maxTemp = jsonObject.getDouble("temp_max"); final double humidity = jsonObject.getDouble("humidity"); final double pressure = jsonObject.getDouble("pressure"); final WeatherData.Main main = new WeatherData.Main(temp, minTemp, @@ -45,9 +49,18 @@ public class JPOSWeatherParser implements IJPOSWeatherParser { jsonObject = jsonWeatherData.getJSONObject("wind"); final double speed = jsonObject.getDouble("speed"); final double deg = jsonObject.getDouble("deg"); - final double gust = jsonObject.getDouble("gust"); - final double var_beg = jsonObject.getDouble("var_beg"); - final double var_end = jsonObject.getDouble("var_end"); + double gust = 0; + try { + gust = jsonObject.getDouble("gust"); + } catch (final JSONException e) {} + double var_beg = 0; + try { + var_beg = jsonObject.getDouble("var_beg"); + } catch (final JSONException e) {} + double var_end = 0; + try { + var_end = jsonObject.getDouble("var_end"); + } catch (final JSONException e) {} final WeatherData.Wind wind = new WeatherData.Wind(speed, deg, gust, var_beg, var_end); @@ -55,7 +68,10 @@ public class JPOSWeatherParser implements IJPOSWeatherParser { final double cloudiness = jsonObject.getDouble("all"); final WeatherData.Clouds clouds = new WeatherData.Clouds(cloudiness); - final double time = jsonObject.getDouble("time"); + double time = 0; + try { + time = jsonObject.getDouble("time"); + } catch (final JSONException e) {} final WeatherData.DataReceivingTime dataReceivingTime = new WeatherData.DataReceivingTime(time); diff --git a/src/de/example/exampletdd/service/WeatherService.java b/src/de/example/exampletdd/service/WeatherService.java index 3416d6e..ed393ca 100644 --- a/src/de/example/exampletdd/service/WeatherService.java +++ b/src/de/example/exampletdd/service/WeatherService.java @@ -46,8 +46,10 @@ public class WeatherService { public String createURIAPIicon(final String icon, final String urlAPI) { final MessageFormat formatURIAPI = new MessageFormat(urlAPI, Locale.ENGLISH); + final Object[] values = new Object[1]; + values[0] = icon; - return formatURIAPI.format(icon); + return formatURIAPI.format(values); } } diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index a863aaa..80c69cf 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionCode="1" android:versionName="1.0" > - +