Update API Interface to pass city, metrics and API key as parameter into method.
Prepared UI with TextViews and ImageViews. Import of all Images as Drawable Resources.
@ -6,6 +6,7 @@ import androidx.appcompat.widget.Toolbar;
|
|||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
@ -44,29 +45,40 @@ public class ShowTemparature extends AppCompatActivity {
|
|||||||
|
|
||||||
textView2 = (TextView) findViewById(R.id.textView2);
|
textView2 = (TextView) findViewById(R.id.textView2);
|
||||||
|
|
||||||
|
// - Build Retrofit API tool
|
||||||
Retrofit retrofit = new Retrofit.Builder()
|
Retrofit retrofit = new Retrofit.Builder()
|
||||||
.baseUrl("https://api.openweathermap.org/data/2.5/")
|
.baseUrl("https://api.openweathermap.org/data/2.5/")
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
// - Build Interface with Retrofit
|
||||||
JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class);
|
JsonApiInterfce jsonApiInterfce = retrofit.create(JsonApiInterfce.class);
|
||||||
Call<Weather> call = jsonApiInterfce.getPost();
|
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>() {
|
call.enqueue(new Callback<Weather>() {
|
||||||
|
|
||||||
|
// - If GET has an answer...
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Call<Weather> call, Response<Weather> response) {
|
public void onResponse(Call<Weather> call, Response<Weather> response) {
|
||||||
|
|
||||||
|
// - Check if that answer is not an error (like 500, 404, etc)
|
||||||
if(! response.isSuccessful()){
|
if(! response.isSuccessful()){
|
||||||
textView2.setText("Code: " + response.code());
|
textView2.setText("Code: " + response.code());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// - If it's all right, update Activity Objects with JSON parsed answer
|
||||||
Weather weather = response.body();
|
Weather weather = response.body();
|
||||||
textView2.append(weather.get_weatherDescription().get_description());
|
textView2.append(weather.get_temps().get_temp_feels_like());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// - If GET has no answer...
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(Call<Weather> call, Throwable t) {
|
public void onFailure(Call<Weather> call, Throwable t) {
|
||||||
|
|
||||||
|
// - Display error message
|
||||||
textView2.setText("Error: " + t.getMessage());
|
textView2.setText("Error: " + t.getMessage());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,11 +3,11 @@ package fr.romanet.vj.apps.myweather.network;
|
|||||||
import fr.romanet.vj.apps.myweather.weather.Weather;
|
import fr.romanet.vj.apps.myweather.weather.Weather;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
import retrofit2.http.GET;
|
import retrofit2.http.GET;
|
||||||
|
import retrofit2.http.Path;
|
||||||
|
import retrofit2.http.Query;
|
||||||
|
|
||||||
public interface JsonApiInterfce {
|
public interface JsonApiInterfce {
|
||||||
|
|
||||||
// @GET("?q=madrid&units=metric&appid=0ac5802392867db8e1ae4f91529f7890");
|
@GET("weather")
|
||||||
|
Call<Weather> getPost(@Query("q") String city, @Query("units") String units, @Query("appid") String appid);
|
||||||
@GET("weather?q=paris&units=metric&appid=0ac5802392867db8e1ae4f91529f7890")
|
|
||||||
Call<Weather> getPost();
|
|
||||||
}
|
}
|
||||||
|
|||||||
10
app/src/main/res/drawable/ic_baseline_arrow_downward_24.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24"
|
||||||
|
android:tint="?attr/colorControlNormal">
|
||||||
|
<path
|
||||||
|
android:fillColor="@android:color/white"
|
||||||
|
android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"/>
|
||||||
|
</vector>
|
||||||
BIN
app/src/main/res/drawable/img50d.png
Normal file
|
After Width: | Height: | Size: 650 B |
BIN
app/src/main/res/drawable/img50n.png
Normal file
|
After Width: | Height: | Size: 650 B |
BIN
app/src/main/res/drawable/img_01d.png
Normal file
|
After Width: | Height: | Size: 948 B |
BIN
app/src/main/res/drawable/img_01n.png
Normal file
|
After Width: | Height: | Size: 945 B |
BIN
app/src/main/res/drawable/img_02d.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
app/src/main/res/drawable/img_02n.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
app/src/main/res/drawable/img_03d.png
Normal file
|
After Width: | Height: | Size: 837 B |
BIN
app/src/main/res/drawable/img_03n.png
Normal file
|
After Width: | Height: | Size: 837 B |
BIN
app/src/main/res/drawable/img_04d.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
app/src/main/res/drawable/img_04n.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
app/src/main/res/drawable/img_09d.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
app/src/main/res/drawable/img_09n.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
app/src/main/res/drawable/img_10d.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
app/src/main/res/drawable/img_10n.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
app/src/main/res/drawable/img_11d.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
app/src/main/res/drawable/img_11n.png
Normal file
|
After Width: | Height: | Size: 2.8 KiB |
BIN
app/src/main/res/drawable/img_13d.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
app/src/main/res/drawable/img_13n.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
@ -29,9 +29,81 @@
|
|||||||
android:id="@+id/textView2"
|
android:id="@+id/textView2"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="TextView"
|
android:layout_marginTop="56dp"
|
||||||
|
android:text="My City"
|
||||||
|
android:textSize="50sp"
|
||||||
|
android:textStyle="bold"
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintHorizontal_bias="0.497"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" />
|
app:layout_constraintTop_toBottomOf="@+id/appBarLayout2" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageView2"
|
||||||
|
android:layout_width="167dp"
|
||||||
|
android:layout_height="100dp"
|
||||||
|
android:layout_marginTop="60dp"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/textView2"
|
||||||
|
app:srcCompat="@drawable/ic_baseline_umbrella_24" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_temp"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="32dp"
|
||||||
|
android:text="X °C"
|
||||||
|
android:textSize="36sp"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_feelslike"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:text=" feels like x °C"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/tv_temp" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_tempmin"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginStart="75dp"
|
||||||
|
android:layout_marginTop="40dp"
|
||||||
|
android:text="Min. X°C"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/tv_feelslike" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tv_tempmax"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="40dp"
|
||||||
|
android:layout_marginEnd="70dp"
|
||||||
|
android:text="Max. X°C"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toBottomOf="@+id/tv_feelslike" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageView4"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
app:layout_constraintEnd_toStartOf="@+id/tv_tempmin"
|
||||||
|
app:layout_constraintTop_toTopOf="@+id/tv_tempmin"
|
||||||
|
app:srcCompat="@drawable/ic_baseline_arrow_downward_24" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/imageView5"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:rotation="180"
|
||||||
|
app:layout_constraintEnd_toStartOf="@+id/tv_tempmax"
|
||||||
|
app:layout_constraintTop_toTopOf="@+id/tv_tempmax"
|
||||||
|
app:srcCompat="@drawable/ic_baseline_arrow_downward_24" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||