use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class Request method gsonWeather.
private WeatherInfo gsonWeather(URL url) throws IOException {
HttpURLConnection connection1 = (HttpURLConnection) url.openConnection();
connection1.addRequestProperty("x-api-key", new Prefs(context).getWeatherKey());
InputStream content = connection1.getInputStream();
try {
// Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
Gson gson = gsonBuilder.create();
WeatherInfo posts = gson.fromJson(reader, WeatherInfo.class);
System.out.println(gson.toJson(posts));
content.close();
return posts;
} catch (Exception ex) {
Log.e("FetchWeather", "Failed to parse JSON due to: " + ex);
}
return null;
}
use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class FetchWeatherOther method gsonWeather.
private WeatherInfo gsonWeather() throws IOException {
URL day = new URL(builtDay.toString());
HttpURLConnection connection1 = (HttpURLConnection) day.openConnection();
connection1.addRequestProperty("x-api-key", preferences.getWeatherKey());
InputStream content = connection1.getInputStream();
try {
//Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
Gson gson = gsonBuilder.create();
WeatherInfo posts = gson.fromJson(reader, WeatherInfo.class);
System.out.println(gson.toJson(posts));
content.close();
return posts;
} catch (Exception ex) {
Log.e(LOG_TAG, "Failed to parse JSON due to: " + ex);
}
return null;
}
use of com.a5corp.weather.model.WeatherInfo in project Weather by Sparker0i.
the class FetchWeatherOther method doInBackground.
@Override
public WeatherInfo doInBackground(String... params) {
city(params);
try {
Log.d(LOG_TAG, "Execution");
URL day = new URL(builtDay.toString());
Log.i("day", day.toString());
Log.d(LOG_TAG, "URI Ready");
WeatherInfo weather = gsonWeather();
if (weather.getCod() != 200) {
Log.e(LOG_TAG, "cod is not 200");
return null;
}
return weather;
} catch (IOException e) {
Log.e(LOG_TAG, "Execution Failed IO");
e.printStackTrace();
return null;
}
}
Aggregations