query weather api and add to database -working
This commit is contained in:
parent
3873885197
commit
0ad83e6488
|
@ -16,6 +16,10 @@ android {
|
|||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = '1.8'
|
||||
targetCompatibility = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package at.fhj.swd.dailyhelper.db.room;
|
||||
|
||||
public class Converters {
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package at.fhj.swd.dailyhelper.db.room;
|
||||
|
||||
import android.arch.persistence.room.Dao;
|
||||
import android.arch.persistence.room.Insert;
|
||||
import android.arch.persistence.room.OnConflictStrategy;
|
||||
import android.arch.persistence.room.Query;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import at.fhj.swd.dailyhelper.model.Weather;
|
||||
|
||||
|
||||
@Dao
|
||||
public interface WeatherDAO {
|
||||
|
||||
@Query("SELECT * FROM weather")
|
||||
List<Weather> getAll();
|
||||
|
||||
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void addWeather(Weather weather);
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
void addAll(List<Weather> weathers);
|
||||
|
||||
@Query("DELETE FROM weather")
|
||||
void clear();
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package at.fhj.swd.dailyhelper.db.room;
|
||||
|
||||
import android.arch.persistence.room.Database;
|
||||
import android.arch.persistence.room.Room;
|
||||
import android.arch.persistence.room.RoomDatabase;
|
||||
import android.content.Context;
|
||||
|
||||
import at.fhj.swd.dailyhelper.model.Weather;
|
||||
|
||||
@Database(entities = {Weather.class}, version = 1,exportSchema = false)
|
||||
public abstract class WeatherDB extends RoomDatabase {
|
||||
|
||||
private static WeatherDB instance;
|
||||
public abstract WeatherDAO weatherDAO();
|
||||
|
||||
|
||||
|
||||
|
||||
public static WeatherDB getDatabase(Context context){
|
||||
if (instance == null) {
|
||||
instance = Room.databaseBuilder(context, WeatherDB.class, "dailyhelper").allowMainThreadQueries().build();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package at.fhj.swd.dailyhelper.model;
|
||||
|
||||
import android.arch.persistence.room.ColumnInfo;
|
||||
import android.arch.persistence.room.Entity;
|
||||
import android.arch.persistence.room.PrimaryKey;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
@Entity(tableName = "weather")
|
||||
public class Weather {
|
||||
@PrimaryKey
|
||||
@NonNull
|
||||
@ColumnInfo(name = "date")
|
||||
private String date;
|
||||
@ColumnInfo(name = "description")
|
||||
private String description;
|
||||
@ColumnInfo(name = "icon")
|
||||
private String icon;
|
||||
@ColumnInfo(name = "temp")
|
||||
private double temp;
|
||||
@ColumnInfo(name = "humidity")
|
||||
private double humidity;
|
||||
|
||||
|
||||
public Weather(@NonNull String date, String description, String icon, double temp, double humidity) {
|
||||
this.date = date;
|
||||
this.description = description;
|
||||
this.icon = icon;
|
||||
this.temp = temp;
|
||||
this.humidity = humidity;
|
||||
}
|
||||
|
||||
public String getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setDate(String date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public void setIcon(String icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public double getTemp() {
|
||||
return temp;
|
||||
}
|
||||
|
||||
public void setTemp(double temp) {
|
||||
this.temp = temp;
|
||||
}
|
||||
|
||||
public double getHumidity() {
|
||||
return humidity;
|
||||
}
|
||||
|
||||
public void setHumidity(double humidity) {
|
||||
this.humidity = humidity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return date + ", " + temp + ", " + humidity;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,13 @@
|
|||
package at.fhj.swd.dailyhelper.ui.activity;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
|
@ -16,16 +20,31 @@ import android.support.v7.app.AppCompatActivity;
|
|||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.Menu;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import at.fhj.swd.dailyhelper.R;
|
||||
import at.fhj.swd.dailyhelper.db.room.WeatherDB;
|
||||
import at.fhj.swd.dailyhelper.model.Weather;
|
||||
import at.fhj.swd.dailyhelper.network.HttpsGetTask;
|
||||
import at.fhj.swd.dailyhelper.network.RequestCallback;
|
||||
import at.fhj.swd.dailyhelper.service.LocationService;
|
||||
import at.fhj.swd.dailyhelper.ui.fragemnt.HomeFragment;
|
||||
import at.fhj.swd.dailyhelper.ui.fragemnt.MapFragment;
|
||||
import at.fhj.swd.dailyhelper.ui.fragemnt.WeatherFragment;
|
||||
|
||||
public class MainActivity extends AppCompatActivity
|
||||
implements NavigationView.OnNavigationItemSelectedListener {
|
||||
implements NavigationView.OnNavigationItemSelectedListener, RequestCallback {
|
||||
|
||||
private DrawerLayout drawer;
|
||||
private ProgressDialog progressDialog;
|
||||
private SharedPreferences preferences;
|
||||
private WeatherFragment weatherFragment;
|
||||
private WeatherDB database;
|
||||
|
||||
|
||||
@Override
|
||||
|
@ -36,6 +55,11 @@ public class MainActivity extends AppCompatActivity
|
|||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
progressDialog = new ProgressDialog(this);
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
database = WeatherDB.getDatabase(this);
|
||||
|
||||
|
||||
drawer = findViewById(R.id.drawer_layout);
|
||||
NavigationView navigationView = findViewById(R.id.nav_view);
|
||||
|
@ -51,6 +75,7 @@ public class MainActivity extends AppCompatActivity
|
|||
navigationView.setCheckedItem(R.id.nav_home);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -107,6 +132,7 @@ public class MainActivity extends AppCompatActivity
|
|||
break;
|
||||
case R.id.nav_weather:
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new WeatherFragment()).commit();
|
||||
fetchWeather();
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -137,4 +163,92 @@ public class MainActivity extends AppCompatActivity
|
|||
|
||||
}
|
||||
|
||||
private void updateFragments() {
|
||||
runOnUiThread(() -> {
|
||||
weatherFragment.refresh();
|
||||
});
|
||||
}
|
||||
|
||||
private void dismissProgressDialog() {
|
||||
runOnUiThread(()-> progressDialog.dismiss());
|
||||
}
|
||||
|
||||
|
||||
private void fetchWeather() {
|
||||
HttpsGetTask httpsGetTask = new HttpsGetTask(this);
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
float latitude = preferences.getFloat("latitude", -1000);
|
||||
float longitude = preferences.getFloat("longitude", -1000);
|
||||
|
||||
if (latitude == -1000 || longitude == -1000) {
|
||||
httpsGetTask.execute("https://api.openaq.org/v1/locations?country=AT&limit=200");
|
||||
} else {
|
||||
httpsGetTask.execute("https://api.openweathermap.org/data/2.5/forecast?lat="+ latitude +"&lon=" + longitude + "&appid=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRequestStart() {
|
||||
progressDialog.setMessage("Fetching weather data...");
|
||||
progressDialog.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResult(String result) {
|
||||
new Thread(()->{
|
||||
|
||||
List<Weather> weather;
|
||||
//parse result
|
||||
if(result == null){
|
||||
System.out.println("no weather");
|
||||
}else {
|
||||
try {
|
||||
weather = parseWeather(result);
|
||||
database.weatherDAO().clear();
|
||||
database.weatherDAO().addAll(weather);
|
||||
}catch (JSONException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//notify fragments
|
||||
dismissProgressDialog();
|
||||
//TODO
|
||||
//updateFragments();
|
||||
|
||||
}).start();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private List<Weather> parseWeather(String json) throws JSONException {
|
||||
|
||||
List<Weather> weather = new ArrayList<>();
|
||||
JSONObject jsonObject = new JSONObject(json);
|
||||
JSONArray results = jsonObject.getJSONArray("list");
|
||||
|
||||
|
||||
for (int i = 0; i < results.length(); i++){
|
||||
JSONObject weatherResult = results.getJSONObject(i);
|
||||
String date = weatherResult.getString("dt_txt");
|
||||
|
||||
JSONArray weatherMetadata = weatherResult.getJSONArray("weather");
|
||||
JSONObject weatherMetadata0 = weatherMetadata.getJSONObject(0);
|
||||
|
||||
String description = weatherMetadata0.getString("description");
|
||||
String icon = weatherMetadata0.getString("icon");
|
||||
|
||||
JSONObject main = weatherResult.getJSONObject("main");
|
||||
|
||||
double temp = main.getDouble("temp");
|
||||
double humidity = main.getDouble("humidity");
|
||||
|
||||
Weather weatherData = new Weather(date, description, icon, temp, humidity);
|
||||
weather.add(weatherData);
|
||||
}
|
||||
return weather;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package at.fhj.swd.dailyhelper.ui.fragemnt;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.SharedPreferences;
|
||||
import android.location.Location;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
@ -8,10 +12,18 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import at.fhj.swd.dailyhelper.R;
|
||||
import at.fhj.swd.dailyhelper.network.HttpsGetTask;
|
||||
import at.fhj.swd.dailyhelper.network.RequestCallback;
|
||||
|
||||
public class WeatherFragment extends Fragment {
|
||||
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
|
@ -20,4 +32,15 @@ public class WeatherFragment extends Fragment {
|
|||
return view;
|
||||
}
|
||||
|
||||
public void refresh(){
|
||||
fetchLocations();
|
||||
System.out.println("test99999999999999999999999999999999");
|
||||
}
|
||||
|
||||
private void fetchLocations() {
|
||||
System.out.println("test99999955555555555");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="@android:color/holo_red_light">
|
||||
android:background="@android:color/darker_gray">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
|
@ -11,4 +11,26 @@
|
|||
android:textSize="80px"/>
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/ly_weather"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_below="@+id/ly_position"
|
||||
android:layout_margin="20dp"
|
||||
android:padding="20dp"
|
||||
android:background="@drawable/border">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_weather"
|
||||
android:text="Wetter"
|
||||
android:padding="10dp"
|
||||
android:textSize="25sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
||||
</RelativeLayout>
|
|
@ -22,7 +22,7 @@ In der ersten Ausbaustufe wird die aktuelle Wetterinformation anhand der GPS Pos
|
|||
### Pflichtenheft
|
||||
|
||||
- [ ] Beinhaltet Netzwerkkommunikation
|
||||
- [ ] Greift auf Gerätefunktionalität zu
|
||||
- [x] Greift auf Gerätefunktionalität zu
|
||||
- [ ] Speichert Daten für offline Verwendung
|
||||
- [ ] Läuft auf (echten) Smartphones, Android 4.4+
|
||||
- [ ] Abgabe in Form von Source-Code + APK + Dokumentation
|
||||
|
|
|
@ -9,5 +9,25 @@
|
|||
|
||||
OpenWeathermap API-Key: `ef4ee3f556d464979db1cfa6fe60f7a0`
|
||||
|
||||
`http://api.openweathermap.org/data/2.5/weather?lat=47&lon=15&APPID=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric`
|
||||
`https://api.openweathermap.org/data/2.5/weather?lat=47&lon=15&APPID=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric`
|
||||
|
||||
Vorhersage:
|
||||
`https://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139&appid=030cadd0e9d6bdb9ae54ff0f841258ea&lang=de&units=metric`
|
||||
|
||||
|
||||
weather
|
||||
0
|
||||
id 502
|
||||
main "Rain"
|
||||
description "Starker Regen"
|
||||
icon "10d"
|
||||
clouds
|
||||
all 100
|
||||
wind
|
||||
speed 5.45
|
||||
deg 51.237
|
||||
rain
|
||||
3h 21.938
|
||||
sys
|
||||
pod "d"
|
||||
dt_txt "2019-06-23 21:00:00"
|
Loading…
Reference in New Issue