@@ -5,13 +5,17 @@ import android.os.Bundle;
55import android.os.Handler;
66import android.os.Looper;
77
8+ import androidx.annotation.Keep;
9+ import androidx.annotation.NonNull;
810import androidx.annotation.Nullable;
911
12+ import com.d4rk.androidtutorials.java.R;
1013import com.d4rk.androidtutorials.java.databinding.ActivityRetrofitBinding;
1114import com.d4rk.androidtutorials.java.ui.components.navigation.UpNavigationActivity;
1215import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate;
16+ import com.google.gson.annotations.SerializedName;
1317
14- import com.d4rk.androidtutorials. java.R ;
18+ import java.util.Map ;
1519
1620import retrofit2.Call;
1721import retrofit2.Callback;
@@ -21,19 +25,10 @@ import retrofit2.converter.gson.GsonConverterFactory;
2125import retrofit2.http.GET;
2226
2327public class RetrofitActivity extends UpNavigationActivity {
24-     private ActivityRetrofitBinding binding;
2528    private final Handler handler = new Handler(Looper.getMainLooper());
29+     private ActivityRetrofitBinding binding;
2630    private JsonPlaceholderApi api;
2731
28-     interface JsonPlaceholderApi {
29-         @GET("todos/1")
30-         Call<Todo> getTodo();
31-     }
32- 
33-     static class Todo {
34-         public String title;
35-     }
36- 
3732    @Override
3833    protected void onCreate(@Nullable Bundle savedInstanceState) {
3934        super.onCreate(savedInstanceState);
@@ -50,20 +45,20 @@ public class RetrofitActivity extends UpNavigationActivity {
5045
5146        binding.buttonFetch.setOnClickListener(v -> {
5247            binding.buttonFetch.setEnabled(false);
53-             api.getTodo().enqueue(new Callback<Todo >() {
48+             api.getTodo().enqueue(new Callback<>() {
5449                @Override
55-                 public void onResponse(Call<Todo> call, Response<Todo> response) {
56-                     if (response.isSuccessful() && response.body() != null ) {
57-                         binding.textViewResult.setText (response.body().title );
50+                 public void onResponse(@NonNull  Call<Todo> call, @NonNull  Response<Todo> response) {
51+                     if (response.isSuccessful()) {
52+                         displayTodoTitle (response);
5853                    } else {
59-                         binding.textViewResult.setText(R.string.snack_general_error );
54+                         showGeneralErrorMessage( );
6055                    }
6156                    binding.buttonFetch.setEnabled(true);
6257                }
6358
6459                @Override
65-                 public void onFailure(Call<Todo> call, Throwable t) {
66-                     binding.textViewResult.setText(R.string.snack_general_error );
60+                 public void onFailure(@NonNull  Call<Todo> call, @NonNull  Throwable t) {
61+                     showGeneralErrorMessage( );
6762                    binding.buttonFetch.setEnabled(true);
6863                }
6964            });
@@ -79,4 +74,38 @@ public class RetrofitActivity extends UpNavigationActivity {
7974        super.onDestroy();
8075        handler.removeCallbacksAndMessages(null);
8176    }
77+ 
78+     private void displayTodoTitle(@NonNull Response<Todo> response) {
79+         Object body = response.body();
80+         if (body instanceof Todo) {
81+             Todo todo = (Todo) body;
82+             if (todo.title != null && !todo.title.isEmpty()) {
83+                 binding.textViewResult.setText(todo.title);
84+                 return;
85+             }
86+         } else if (body instanceof Map<?, ?> map) {
87+             Object title = map.get("title");
88+             if (title != null) {
89+                 binding.textViewResult.setText(String.valueOf(title));
90+                 return;
91+             }
92+         }
93+         showGeneralErrorMessage();
94+     }
95+ 
96+     private void showGeneralErrorMessage() {
97+         binding.textViewResult.setText(R.string.snack_general_error);
98+     }
99+ 
100+     interface JsonPlaceholderApi {
101+         @GET("todos/1")
102+         Call<Todo> getTodo();
103+     }
104+ 
105+     @Keep
106+     public static final class Todo {
107+         @SerializedName("title")
108+         @Nullable
109+         public String title;
110+     }
82111}
0 commit comments