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.
This commit is contained in:
valentin 2020-11-15 17:41:41 +01:00
parent cb7db29f87
commit 4b5415895b
22 changed files with 101 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
@ -44,29 +45,40 @@ public class ShowTemparature extends AppCompatActivity {
textView2 = (TextView) findViewById(R.id.textView2);
// - 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();
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_weatherDescription().get_description());
textView2.append(weather.get_temps().get_temp_feels_like());
}
// - If GET has no answer...
@Override
public void onFailure(Call<Weather> call, Throwable t) {
// - Display error message
textView2.setText("Error: " + t.getMessage());
}
});

View File

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

View 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>

Binary file not shown.

After

Width:  |  Height:  |  Size: 650 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 650 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 948 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 945 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -29,9 +29,81 @@
android:id="@+id/textView2"
android:layout_width="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_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
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>