Skip to content

Commit

Permalink
[In Progress] building the WeatherDataBuilder class
Browse files Browse the repository at this point in the history
  • Loading branch information
kalyandechiraju committed Jan 11, 2016
1 parent a6706e3 commit 9b67d88
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 37 deletions.
2 changes: 1 addition & 1 deletion weatherdownloaderlibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package="studios.codelight.weatherdownloaderlibrary">

<application
android:allowBackup="true"
android:allowBackup="false"
android:label="@string/app_name"
android:supportsRtl="true">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.net.HttpURLConnection;
import java.net.URL;


@Deprecated
public class DownloadData extends AsyncTask<String, Void, String> {
private static final String LOGTAG = "DownloadData";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package studios.codelight.weatherdownloaderlibrary;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

package studios.codelight.weatherdownloaderlibrary;

import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

Expand All @@ -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());
Expand All @@ -60,30 +69,42 @@ 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;
}
return null;
}

public interface WeatherDataDownloadListener {
String postDownload();
void onWeatherDownloadComplete(WeatherData data);
void onWeatherDownloadFailed(Exception e);
}


private class DownloadCurrentData extends AsyncTask<String, Void, String> {
private static final String LOGTAG = "DownloadData";

@Override
protected String doInBackground(String... params) {
InputStream inputStream = null;
Expand All @@ -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) {
Expand All @@ -106,7 +127,7 @@ protected String doInBackground(String... params) {
httpURLConnection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.getMessage());
}
}
return null;
Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 9b67d88

Please sign in to comment.