Retrofit is a type-safe HTTP client for Android and Java. It makes it easy to send HTTP requests and process the response. It also provides support for converting JSON data to Java objects and vice versa.Here's an example of how you can use Retrofit to make an API request and process the response:
Copy dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
}
Copy public class Todo {
private int userId;
private int id;
private String title;
private boolean completed;
// Getter and Setter methods
}
Copy public class TodoApiHelper {
private static final String BASE_URL = "https://jsonplaceholder.typicode.com/" ;
private Retrofit retrofit;
public TodoApiHelper () {
retrofit = new Retrofit . Builder ()
. baseUrl (BASE_URL)
. addConverterFactory ( GsonConverterFactory . create ())
. build ();
}
public void getTodoItem ( int todoId , Callback < Todo > callback) {
TodoApiService todoApiService = retrofit . create ( TodoApiService . class );
Call < Todo > call = todoApiService . getTodoItem (todoId);
call . enqueue (callback);
}
}
Copy public interface TodoApiService {
@ GET ( "todos/{id}" )
Call < Todo > getTodoItem (@ Path ( "id" ) int todoId);
}
Copy public class MainActivity extends AppCompatActivity {
private TextView titleTextView;
private TextView completedTextView;
private TodoApiHelper todoApiHelper;
@ Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate (savedInstanceState);
setContentView( R . layout . activity_main ) ;
titleTextView = findViewById( R . id . titleTextView ) ;
completedTextView = findViewById( R . id . completedTextView ) ;
todoApiHelper = new TodoApiHelper() ;
int todoId = 1 ; // The ID of the todo item you want to fetch
todoApiHelper . getTodoItem (todoId , new Callback < Todo >() {
@ Override
public void onResponse ( Call < Todo > call , Response < Todo > response) {
if ( response . isSuccessful ()) {
Todo todo = response . body ();
updateUI(todo) ;
} else {
// Handle API error
}
}
@ Override
public void onFailure ( Call < Todo > call , Throwable t) {
// Handle API failure
}
});
}
private void updateUI ( Todo todo) {
titleTextView . setText ( todo . getTitle ());
completedTextView . setText ( String . valueOf ( todo . isCompleted ()));
}
}
Copy <? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout xmlns : android = "http://schemas.android.com/apk/res/android"
xmlns : app = "http://schemas.android.com/apk/res-auto"
xmlns : tools = "http://schemas.android.com/tools"
android : layout_width = "match_parent"
android : layout_height = "match_parent"
tools : context = ".MainActivity" >
< TextView
android : id = "@+id/titleTextView"
android : layout_width = "wrap_content"
android : layout_height = "wrap_content"
android : layout_marginTop = "320dp"
android : text = "TextView"
android : textSize = "30sp"
app : layout_constraintEnd_toEndOf = "parent"
app : layout_constraintHorizontal_bias = "0.498"
app : layout_constraintStart_toStartOf = "parent"
app : layout_constraintTop_toTopOf = "parent" />
< TextView
android : id = "@+id/completedTextView"
android : layout_width = "wrap_content"
android : layout_height = "wrap_content"
android : text = "TextView"
android : textSize = "30sp"
app : layout_constraintBottom_toBottomOf = "parent"
app : layout_constraintEnd_toEndOf = "parent"
app : layout_constraintHorizontal_bias = "0.498"
app : layout_constraintStart_toStartOf = "parent"
app : layout_constraintTop_toBottomOf = "@+id/titleTextView"
app : layout_constraintVertical_bias = "0.23" />
</ androidx.constraintlayout.widget.ConstraintLayout >
Remember to add the necessary permissions and configurations for network access in your AndroidManifest.xml file.
Copy < uses-permission android : name = "android.permission.INTERNET" />
This code provides a basic implementation that can be expanded upon for a more robust solution.