Update of all weather Classes (Some deleted, some modified)

Setup of Retrofit API and API Interface
Added test TextView2 on activity_temparature.xml in order to test API answer
Display Paris weather description on any city we click for the moment.
This commit is contained in:
valentin 2020-11-15 15:27:05 +01:00
parent 49871926bd
commit cb7db29f87
10 changed files with 116 additions and 35 deletions

View File

@ -29,5 +29,5 @@
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest> </manifest>

View File

@ -8,16 +8,25 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.TextView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import fr.romanet.vj.apps.myweather.bo.City; import fr.romanet.vj.apps.myweather.bo.City;
import fr.romanet.vj.apps.myweather.network.JsonApiInterfce;
import fr.romanet.vj.apps.myweather.repository.CityRepository; import fr.romanet.vj.apps.myweather.repository.CityRepository;
import fr.romanet.vj.apps.myweather.weather.Weather;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ShowTemparature extends AppCompatActivity { public class ShowTemparature extends AppCompatActivity {
private String currentCityName; private String currentCityName;
private TextView textView2;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -32,6 +41,36 @@ public class ShowTemparature extends AppCompatActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true);
textView2 = (TextView) findViewById(R.id.textView2);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.openweathermap.org/data/2.5/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class);
Call<Weather> call = jsonApiInterfce.getPost();
call.enqueue(new Callback<Weather>() {
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
if(! response.isSuccessful()){
textView2.setText("Code: " + response.code());
return;
}
Weather weather = response.body();
textView2.append(weather.get_weatherDescription().get_description());
}
@Override
public void onFailure(Call<Weather> call, Throwable t) {
textView2.setText("Error: " + t.getMessage());
}
});
} }
@Override @Override

View File

@ -1,11 +0,0 @@
package fr.romanet.vj.apps.myweather.network;
import retrofit2.Retrofit;
public class ApiClient {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.build();
GitHubService service = retrofit.create(GitHubService.class);
}

View File

@ -1,4 +0,0 @@
package fr.romanet.vj.apps.myweather.network;
public class Get {
}

View File

@ -0,0 +1,13 @@
package fr.romanet.vj.apps.myweather.network;
import fr.romanet.vj.apps.myweather.weather.Weather;
import retrofit2.Call;
import retrofit2.http.GET;
public interface JsonApiInterfce {
// @GET("?q=madrid&units=metric&appid=0ac5802392867db8e1ae4f91529f7890");
@GET("weather?q=paris&units=metric&appid=0ac5802392867db8e1ae4f91529f7890")
Call<Weather> getPost();
}

View File

@ -1,16 +0,0 @@
package fr.romanet.vj.apps.myweather.weather;
import com.google.gson.annotations.SerializedName;
public class Coord {
@SerializedName("lon")
private String _lon;
@SerializedName("lat")
private String _lat;
public Coord(String lon, String lat){
_lon = lon;
_lat = lat;
}
}

View File

@ -19,4 +19,21 @@ public class Temps {
_temp_min = temp_min; _temp_min = temp_min;
_temp_max = temp_max; _temp_max = temp_max;
} }
public String get_temp() {
return _temp;
}
public String get_temp_feels_like() {
return _temp_feels_like;
}
public String get_temp_min() {
return _temp_min;
}
public String get_temp_max() {
return _temp_max;
}
} }

View File

@ -2,13 +2,39 @@ package fr.romanet.vj.apps.myweather.weather;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Weather { public class Weather {
@SerializedName("coord")
private Coord _coord;
@SerializedName("main") @SerializedName("main")
private Temps _temps; private Temps _temps;
@SerializedName("weather") @SerializedName("weather")
private WeatherDescription _weatherDescription; private List<WeatherDescription> _weatherDescription;
@SerializedName("name")
private String _city_name;
@SerializedName("id")
private String _city_id;
public Weather(Temps temps, List<WeatherDescription> weatherDescription, String city_name, String city_id){
_temps = temps;
_weatherDescription = weatherDescription;
_city_name = city_name;
_city_id = city_id;
}
public Temps get_temps() {
return _temps;
}
public WeatherDescription get_weatherDescription() {
return _weatherDescription.get(0);
}
public String get_city_name() {
return _city_name;
}
public String get_city_id() {
return _city_id;
}
} }

View File

@ -14,4 +14,11 @@ public class WeatherDescription {
_icon = icon; _icon = icon;
} }
public String get_description() {
return _description;
}
public String get_icon() {
return _icon;
}
} }

View File

@ -7,6 +7,7 @@
tools:context=".ShowTemparature"> tools:context=".ShowTemparature">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout2"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:theme="@style/Theme.MyWeather.AppBarOverlay" android:theme="@style/Theme.MyWeather.AppBarOverlay"
@ -24,4 +25,13 @@
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>