Retrofit Tutorial SIMPLE GET REQUEST - Android Studio

Retrofit
Make sure to require Internet permissions in your AndroidManifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>
Add the following to your app/build.gradle file:
dependencies {
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'  

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
app:cardCornerRadius="8dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#000"
android:textSize="16sp" />
<TextView
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#000"
android:textSize="13sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
view raw item.xml hosted with ❤ by GitHub
package com.csdevbin.retrofitdemo;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface JsonPlaceHolderApi {
@GET("posts")
Call<List<Post>> getPosts();
}
package com.csdevbin.retrofitdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private String BASE_URL = "https://jsonplaceholder.typicode.com/";
private RecyclerAdapter adapter;
private RecyclerView mRecyclerView;
private LinearLayoutManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mRecyclerView.setAdapter(mAdapter);
retroCall();
}
private void retroCall() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<List<Post>> call = jsonPlaceHolderApi.getPosts();
call.enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
/*if (!response.isSuccessful()) {
}*/
List<Post> posts = response.body();
mRecyclerView = findViewById(R.id.recycler);
manager = new LinearLayoutManager(MainActivity.this);
mRecyclerView.setLayoutManager(manager);
mRecyclerView.setHasFixedSize(true);
adapter = new RecyclerAdapter(posts);
mRecyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
}
});
}
}
package com.csdevbin.retrofitdemo;
import com.google.gson.annotations.SerializedName;
public class Post {
private int userId;
private int id;
private String title;
@SerializedName("body")
private String text;
public int getUserId() {
return userId;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getText() {
return text;
}
}
view raw Post.java hosted with ❤ by GitHub
package com.csdevbin.retrofitdemo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyHolder> {
List<Post> postList;
public RecyclerAdapter(List<Post> postList) {
this.postList = postList;
}
@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item, parent, false);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
Post post = postList.get(position);
holder.title.setText(post.getTitle());
holder.body.setText(post.getText());
}
@Override
public int getItemCount() {
return postList.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView body;
public MyHolder(@NonNull View view) {
super(view);
title = view.findViewById(R.id.title);
body = view.findViewById(R.id.body);
}
}
}

Comments :

Post a Comment