Trying to update localDB from API

This commit is contained in:
valentin 2020-11-15 20:18:25 +01:00
parent 58d29974f5
commit 01572fb0c2

View File

@ -70,7 +70,10 @@ public class ShowTemparature extends AppCompatActivity {
// - If it's all right, update Activity Objects with JSON parsed answer // - If it's all right, update Activity Objects with JSON parsed answer
Weather weather = response.body(); Weather weather = response.body();
textView2.append(weather.get_temps().get_temp_feels_like()); // textView2.append(weather.get_temps().get_temp_feels_like());
updateCity(weather);
updateActivityObjectsFromDB();
} }
@ -85,6 +88,53 @@ public class ShowTemparature extends AppCompatActivity {
} }
@Override
protected void onResume() {
super.onResume();
// - Build Retrofit API tool
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();
// - Build Interface with Retrofit
JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class);
Call<Weather> call = jsonApiInterfce.getPost(currentCityName, "metric", "0ac5802392867db8e1ae4f91529f7890");
// - 'Execute' the GET query with 'enqueue' method, Retrofit will create an AsyncTask that avoids UI Thread freeze Exception.
call.enqueue(new Callback<Weather>() {
// - If GET has an answer...
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
// - Check if that answer is not an error (like 500, 404, etc)
if(! response.isSuccessful()){
textView2.setText("Code: " + response.code());
return;
}
// - If it's all right, update Activity Objects with JSON parsed answer
Weather weather = response.body();
// textView2.append(weather.get_temps().get_temp_feels_like());
updateCity(weather);
updateActivityObjectsFromDB();
}
// - If GET has no answer...
@Override
public void onFailure(Call<Weather> call, Throwable t) {
// - Display error message
textView2.setText("Error: " + t.getMessage());
}
});
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items, menu); getMenuInflater().inflate(R.menu.menu_items, menu);
@ -116,4 +166,30 @@ public class ShowTemparature extends AppCompatActivity {
} }
} }
} }
public void updateCity(Weather weather){
City updated_city = new City(currentCityName);
updated_city.setW_temp(weather.get_temps().get_temp());
updated_city.setW_feelsliketemp(weather.get_temps().get_temp_feels_like());
updated_city.setW_icon(weather.get_weatherDescription().get_icon());
updated_city.setW_maxtemp(weather.get_temps().get_temp_max());
updated_city.setW_mintemp(weather.get_temps().get_temp_min());
CityRepository.getInstance(this).updateCity(updated_city);
}
public void updateActivityObjectsFromDB(){
final List<City> city = CityRepository.getInstance(this).getCity();
String temp;
for(int i = 0; i < city.size(); i++)
{
if(city.get(i).nameCity.equals(currentCityName))
{
temp = city.get(i).nameCity;
Log.d("DEBUG1", "---------------------------------------->" + temp);
textView2.setText(temp);
CityRepository.getInstance(this).deleteCity(city.get(i));
}
}
}
} }