Search in sources :

Example 6 with WeatherInfo

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;
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) Prefs(com.a5corp.weather.preferences.Prefs) IOException(java.io.IOException)

Example 7 with WeatherInfo

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;
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) URL(java.net.URL) IOException(java.io.IOException)

Example 8 with WeatherInfo

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;
    }
}
Also used : WeatherInfo(com.a5corp.weather.model.WeatherInfo) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

WeatherInfo (com.a5corp.weather.model.WeatherInfo)8 IOException (java.io.IOException)5 PendingIntent (android.app.PendingIntent)4 Gson (com.google.gson.Gson)4 Intent (android.content.Intent)3 Prefs (com.a5corp.weather.preferences.Prefs)3 GsonBuilder (com.google.gson.GsonBuilder)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 SharedPreferences (android.content.SharedPreferences)2 RemoteViews (android.widget.RemoteViews)2 AlarmManager (android.app.AlarmManager)1 WeatherActivity (com.a5corp.weather.activity.WeatherActivity)1 CheckConnection (com.a5corp.weather.internet.CheckConnection)1 Request (com.a5corp.weather.internet.Request)1