From 9b67d887b274c74c1b230285e223cc36f3ba3c74 Mon Sep 17 00:00:00 2001 From: Kalyan Dechiraju Date: Mon, 11 Jan 2016 09:44:13 +0530 Subject: [PATCH] [In Progress] building the WeatherDataBuilder class --- .../src/main/AndroidManifest.xml | 2 +- .../DownloadData.java | 2 +- .../WeatherDataBuilder.java | 119 +++++++++++++++--- .../WeatherDownloader.java | 56 ++++++--- 4 files changed, 142 insertions(+), 37 deletions(-) diff --git a/weatherdownloaderlibrary/src/main/AndroidManifest.xml b/weatherdownloaderlibrary/src/main/AndroidManifest.xml index 1573f78..72c32f1 100644 --- a/weatherdownloaderlibrary/src/main/AndroidManifest.xml +++ b/weatherdownloaderlibrary/src/main/AndroidManifest.xml @@ -2,7 +2,7 @@ package="studios.codelight.weatherdownloaderlibrary"> diff --git a/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/DownloadData.java b/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/DownloadData.java index c8219ec..ce38f81 100644 --- a/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/DownloadData.java +++ b/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/DownloadData.java @@ -37,7 +37,7 @@ import java.net.HttpURLConnection; import java.net.URL; - +@Deprecated public class DownloadData extends AsyncTask { private static final String LOGTAG = "DownloadData"; diff --git a/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDataBuilder.java b/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDataBuilder.java index 0b8e3af..550007f 100644 --- a/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDataBuilder.java +++ b/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDataBuilder.java @@ -1,5 +1,7 @@ package studios.codelight.weatherdownloaderlibrary; +import android.util.Log; + import org.json.JSONException; import org.json.JSONObject; @@ -37,35 +39,112 @@ * Created by kalyan on 9/1/16. */ public class WeatherDataBuilder { - public static WeatherData buildWeatherData(String response) throws JSONException { - JSONObject jsonObject = new JSONObject(response); + private static final String LOG_TAG = "WeatherDownloader"; + public static WeatherData buildWeatherData(String response) { + JSONObject jsonObject; + try { + jsonObject = new JSONObject(response); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + return null; + } WeatherData weatherData = new WeatherData(); - Clouds cloudsData = JsonUtil.getCloudsObjectFromJson(response); - weatherData.setClouds(cloudsData); + Clouds cloudsData = null; + try { + cloudsData = JsonUtil.getCloudsObjectFromJson(response); + weatherData.setClouds(cloudsData); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setClouds(cloudsData); + } + + Coord coordData = null; + try { + coordData = JsonUtil.getCoordObjectFromJson(response); + weatherData.setCoord(coordData); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setCoord(coordData); + } + + Main mainData = null; + try { + mainData = JsonUtil.getMainObjectFromJson(response); + weatherData.setMain(mainData); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setMain(mainData); + } + + Sys sysData = null; + try { + sysData = JsonUtil.getSysObjectFromJson(response); + weatherData.setSys(sysData); + } catch (JSONException e) { + weatherData.setSys(sysData); + Log.e(LOG_TAG, e.getMessage()); + } + - Coord coordData = JsonUtil.getCoordObjectFromJson(response); - weatherData.setCoord(coordData); + Weather[] weatherObjectData = null; + try { + weatherObjectData = JsonUtil.getWeatherObjectFromJson(response); + weatherData.setWeather(weatherObjectData); + } catch (JSONException e) { + weatherData.setWeather(weatherObjectData); + Log.e(LOG_TAG, e.getMessage()); + } - Main mainData = JsonUtil.getMainObjectFromJson(response); - weatherData.setMain(mainData); - Sys sysData = JsonUtil.getSysObjectFromJson(response); - weatherData.setSys(sysData); + Wind windData = null; + try { + windData = JsonUtil.getWindObjectFromJson(response); + weatherData.setWind(windData); + } catch (JSONException e) { + weatherData.setWind(windData); + Log.e(LOG_TAG, e.getMessage()); + } - Weather[] weatherObjectData = JsonUtil.getWeatherObjectFromJson(response); - weatherData.setWeather(weatherObjectData); - Wind windData = JsonUtil.getWindObjectFromJson(response); - weatherData.setWind(windData); + try { + weatherData.setBase(jsonObject.getString("base")); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setBase(null); + } + try { + weatherData.setVisibility(jsonObject.getString("visibility")); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setVisibility(null); + } + try { + weatherData.setDt(jsonObject.getString("dt")); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setDt(null); - weatherData.setBase(jsonObject.getString("base")); - weatherData.setVisibility(jsonObject.getString("visibility")); - weatherData.setDt(jsonObject.getString("dt")); - weatherData.setId(jsonObject.getString("id")); - weatherData.setName(jsonObject.getString("name")); - weatherData.setCod(jsonObject.getString("cod")); + } + try { + weatherData.setId(jsonObject.getString("id")); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setId(null); + } + try { + weatherData.setName(jsonObject.getString("name")); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setName(null); + } + try { + weatherData.setCod(jsonObject.getString("cod")); + } catch (JSONException e) { + Log.e(LOG_TAG, e.getMessage()); + weatherData.setCod(null); + } return weatherData; } diff --git a/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDownloader.java b/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDownloader.java index a2b32f5..da0fff6 100644 --- a/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDownloader.java +++ b/weatherdownloaderlibrary/src/main/java/studios/codelight/weatherdownloaderlibrary/WeatherDownloader.java @@ -26,6 +26,7 @@ package studios.codelight.weatherdownloaderlibrary; +import android.net.Uri; import android.os.AsyncTask; import android.util.Log; @@ -37,20 +38,28 @@ import java.net.HttpURLConnection; import java.net.URL; +import studios.codelight.weatherdownloaderlibrary.model.WeatherData; + public class WeatherDownloader { public static final String LOG_TAG = "WeatherDownloader"; private WeatherDataDownloadListener downloadListener; private Mode mode; + private final String BASE_URL = "api.openweathermap.org"; + private final String DATA_PATH = "data"; + private final String VERSION_PATH = "2.5"; + private final String WEATHER_PATH = "weather"; + + public WeatherDownloader(WeatherDataDownloadListener downloadListener, Mode mode) { this.downloadListener = downloadListener; this.mode = mode; } - public void getWeatherData(String apiKey) { + public void getWeatherData(String apiKey, String query) { if(apiKey != null) { try { - String url = buildUrl(apiKey, mode); + String url = buildUrl(apiKey, mode, query); new DownloadCurrentData().execute(apiKey); } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); @@ -60,16 +69,29 @@ public void getWeatherData(String apiKey) { } } - private String buildUrl(String apiKey, Mode mode) { + private String buildUrl(String apiKey, Mode mode, String query) { + Uri.Builder builder = new Uri.Builder(); + builder.scheme("http") + .authority(BASE_URL) + .appendPath(DATA_PATH) + .appendPath(VERSION_PATH) + .appendPath(WEATHER_PATH) + .appendQueryParameter("appid", apiKey); switch (mode) { case CITYNAME: - break; + builder.appendQueryParameter("q", query); + return builder.build().toString(); case ZIPCODE: - break; + builder.appendQueryParameter("zip", query); + return builder.build().toString(); case COORDINATES: - break; + String[] coord = query.split(":"); + builder.appendQueryParameter("lat", coord[0]); + builder.appendQueryParameter("lon", coord[1]); + return builder.build().toString(); case CITYID: - break; + builder.appendQueryParameter("id", query); + return builder.build().toString(); default: break; } @@ -77,13 +99,12 @@ private String buildUrl(String apiKey, Mode mode) { } public interface WeatherDataDownloadListener { - String postDownload(); + void onWeatherDownloadComplete(WeatherData data); + void onWeatherDownloadFailed(Exception e); } private class DownloadCurrentData extends AsyncTask { - private static final String LOGTAG = "DownloadData"; - @Override protected String doInBackground(String... params) { InputStream inputStream = null; @@ -92,11 +113,11 @@ protected String doInBackground(String... params) { try { url = new URL(params[0]); httpURLConnection = (HttpURLConnection) url.openConnection(); - httpURLConnection.setConnectTimeout(20000); + httpURLConnection.setConnectTimeout(15000); //15 sec inputStream = new BufferedInputStream(httpURLConnection.getInputStream()); return convertInputStreamToString(inputStream); } catch (IOException e) { - Log.e(LOGTAG, e.getMessage()); + Log.e(LOG_TAG, e.getMessage()); } finally { try { if (inputStream != null) { @@ -106,7 +127,7 @@ protected String doInBackground(String... params) { httpURLConnection.disconnect(); } } catch (IOException e) { - e.printStackTrace(); + Log.e(LOG_TAG, e.getMessage()); } } return null; @@ -115,9 +136,14 @@ protected String doInBackground(String... params) { @Override protected void onPostExecute(String response) { if(response == null){ - Log.e(LOGTAG, "Response is null"); + Log.e(LOG_TAG, "Response is null"); } else { - downloadListener.postDownload(); + try { + downloadListener.onWeatherDownloadComplete(WeatherDataBuilder.buildWeatherData(response)); + } catch (Exception e) { + Log.e(LOG_TAG, e.getMessage()); + downloadListener.onWeatherDownloadFailed(e); + } } }