Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.d4rk.androidtutorials.java.R;
import com.d4rk.androidtutorials.java.databinding.ActivityRetrofitBinding;
import com.d4rk.androidtutorials.java.ui.components.navigation.UpNavigationActivity;
import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate;
import com.google.gson.annotations.SerializedName;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
Expand Down Expand Up @@ -47,17 +51,17 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
api.getTodo().enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<Todo> call, @NonNull Response<Todo> response) {
if (response.isSuccessful() && response.body() != null) {
binding.textViewResult.setText(response.body().title);
if (response.isSuccessful()) {
displayTodoTitle(response);
} else {
binding.textViewResult.setText(R.string.snack_general_error);
showGeneralErrorMessage();
}
binding.buttonFetch.setEnabled(true);
}

@Override
public void onFailure(@NonNull Call<Todo> call, @NonNull Throwable t) {
binding.textViewResult.setText(R.string.snack_general_error);
showGeneralErrorMessage();
binding.buttonFetch.setEnabled(true);
}
});
Expand All @@ -74,12 +78,37 @@ protected void onDestroy() {
handler.removeCallbacksAndMessages(null);
}

private void displayTodoTitle(@NonNull Response<Todo> response) {
Object body = response.body();
if (body instanceof Todo) {
Todo todo = (Todo) body;
if (todo.title != null && !todo.title.isEmpty()) {
binding.textViewResult.setText(todo.title);
return;
}
} else if (body instanceof Map<?, ?> map) {
Object title = map.get("title");
if (title != null) {
binding.textViewResult.setText(String.valueOf(title));
return;
}
}
showGeneralErrorMessage();
}

private void showGeneralErrorMessage() {
binding.textViewResult.setText(R.string.snack_general_error);
}

interface JsonPlaceholderApi {
@GET("todos/1")
Call<Todo> getTodo();
}

static class Todo {
@Keep
public static final class Todo {
@SerializedName("title")
@Nullable
public String title;
}
}
65 changes: 47 additions & 18 deletions app/src/main/res/raw/text_retrofit_java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.d4rk.androidtutorials.java.R;
import com.d4rk.androidtutorials.java.databinding.ActivityRetrofitBinding;
import com.d4rk.androidtutorials.java.ui.components.navigation.UpNavigationActivity;
import com.d4rk.androidtutorials.java.utils.EdgeToEdgeDelegate;
import com.google.gson.annotations.SerializedName;

import com.d4rk.androidtutorials.java.R;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
Expand All @@ -21,19 +25,10 @@ import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;

public class RetrofitActivity extends UpNavigationActivity {
private ActivityRetrofitBinding binding;
private final Handler handler = new Handler(Looper.getMainLooper());
private ActivityRetrofitBinding binding;
private JsonPlaceholderApi api;

interface JsonPlaceholderApi {
@GET("todos/1")
Call<Todo> getTodo();
}

static class Todo {
public String title;
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -50,20 +45,20 @@ public class RetrofitActivity extends UpNavigationActivity {

binding.buttonFetch.setOnClickListener(v -> {
binding.buttonFetch.setEnabled(false);
api.getTodo().enqueue(new Callback<Todo>() {
api.getTodo().enqueue(new Callback<>() {
@Override
public void onResponse(Call<Todo> call, Response<Todo> response) {
if (response.isSuccessful() && response.body() != null) {
binding.textViewResult.setText(response.body().title);
public void onResponse(@NonNull Call<Todo> call, @NonNull Response<Todo> response) {
if (response.isSuccessful()) {
displayTodoTitle(response);
} else {
binding.textViewResult.setText(R.string.snack_general_error);
showGeneralErrorMessage();
}
binding.buttonFetch.setEnabled(true);
}

@Override
public void onFailure(Call<Todo> call, Throwable t) {
binding.textViewResult.setText(R.string.snack_general_error);
public void onFailure(@NonNull Call<Todo> call, @NonNull Throwable t) {
showGeneralErrorMessage();
binding.buttonFetch.setEnabled(true);
}
});
Expand All @@ -79,4 +74,38 @@ public class RetrofitActivity extends UpNavigationActivity {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}

private void displayTodoTitle(@NonNull Response<Todo> response) {
Object body = response.body();
if (body instanceof Todo) {
Todo todo = (Todo) body;
if (todo.title != null && !todo.title.isEmpty()) {
binding.textViewResult.setText(todo.title);
return;
}
} else if (body instanceof Map<?, ?> map) {
Object title = map.get("title");
if (title != null) {
binding.textViewResult.setText(String.valueOf(title));
return;
}
}
showGeneralErrorMessage();
}

private void showGeneralErrorMessage() {
binding.textViewResult.setText(R.string.snack_general_error);
}

interface JsonPlaceholderApi {
@GET("todos/1")
Call<Todo> getTodo();
}

@Keep
public static final class Todo {
@SerializedName("title")
@Nullable
public String title;
}
}