Last active
October 29, 2021 15:26
-
-
Save hmasum52/aef26306193e66cfaed90f2e0ed41990 to your computer and use it in GitHub Desktop.
Fastest way to call API endpoints in android using retrofit2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Api { | |
public static Api instance = new Api(); | |
public Endpoints endpoints; | |
private Api(){ | |
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); | |
interceptor.level(HttpLoggingInterceptor.Level.BODY); | |
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor) | |
.build(); | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://jsonplaceholder.typicode.com/") | |
.client(client) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); | |
endpoints = retrofit.create(Endpoints.class); | |
} | |
public interface Endpoints{ | |
@GET("posts/{id}") // products/1?test="" | |
Call<Post> getPost(@Path("id") int id); | |
@GET("posts") | |
Call<List<Post>> getPosts(); | |
@POST("posts") | |
Call<ResponseBody> postPost(@Body Post post); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependecies{ | |
//retrofit for network calls | |
implementation "com.squareup.retrofit2:retrofit:2.9.0" | |
// use json to pojo: https://www.jsonschema2pojo.org/ | |
implementation "com.squareup.retrofit2:converter-gson:2.9.0" | |
//for debugging with retrofit | |
//for logging http request by retrofit | |
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.1' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
public static final String TAG = "MainActivity->"; | |
ActivityMainBinding mVB; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
mVB = ActivityMainBinding.inflate(getLayoutInflater()); | |
setContentView(mVB.getRoot()); | |
Log.d(TAG, "onCreate: "); | |
Api.Endpoints endpoints = Api.instance.endpoints; | |
Call<Post> call = endpoints.getPost(1); | |
/*call.enqueue(new Callback<Post>() { | |
@Override | |
public void onResponse(Call<Post> call, Response<Post> response) { | |
if(response.isSuccessful() && response.body()!=null){ | |
Post post = response.body(); | |
Log.d(TAG, "onResponse: "+post); | |
}else{ | |
Log.e(TAG, "onResponse: error"); | |
} | |
} | |
@Override | |
public void onFailure(Call<Post> call, Throwable t) { | |
} | |
});*/ | |
Post post = new Post(1, 1, "Tamim", "Tamim ehsan"); | |
Call<ResponseBody> call1 = endpoints.postPost(post); | |
call1.enqueue(new Callback<ResponseBody>() { | |
@Override | |
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { | |
if(response.isSuccessful() && response.body()!=null){ | |
try { | |
String body = response.body().string(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public void onFailure(Call<ResponseBody> call, Throwable t) { | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.annotations.Expose; | |
import com.google.gson.annotations.SerializedName; | |
public class Post { | |
@SerializedName("userId") | |
@Expose | |
private Integer userId; | |
@SerializedName("id") | |
@Expose | |
private Integer id; | |
@SerializedName("title") | |
@Expose | |
private String title; | |
@SerializedName("body") | |
@Expose | |
private String body; | |
public Post(Integer userId, Integer id, String title, String body) { | |
this.userId = userId; | |
this.id = id; | |
this.title = title; | |
this.body = body; | |
} | |
public Integer getUserId() { | |
return userId; | |
} | |
public void setUserId(Integer userId) { | |
this.userId = userId; | |
} | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public String getBody() { | |
return body; | |
} | |
public void setBody(String body) { | |
this.body = body; | |
} | |
@Override | |
public String toString() { | |
return "Post{" + | |
"userId=" + userId + | |
", id=" + id + | |
", title='" + title + '\'' + | |
", body='" + body + '\'' + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment