Search..

Sunday, June 19, 2022

Creating retrofit service

 

public interface MyService {
String BASEURL="https://jsonplaceholder.typicode.com/";
String FEED="posts";

//this logintercetor use for debuging the app



Retrofit retrifit=new Retrofit.Builder()
.baseUrl(BASEURL)
.addConverterFactory(GsonConverterFactory.create())
.client(new RestClient().getClient())
.build();
@GET(FEED)
Call<List<MyPojo>> getPosts();

//for comment
@GET("posts/1/comments")
Call<List<MyComment>> getComent();

@GET("posts/{id}/comments")
Call<List<MyComment>> getComment2(@Path("id")int postid);

/* @GET("comments")
Call<List<MyComment>> getComment3(@Query("postId") Integer[] id,
@Query("_sort") String sortBy,
@Query("_order") String orderBy);*/


@GET("comments")
Call<List<MyComment>> getComment3(@QueryMap Map<String,String> permas);

@POST("posts")
Call<MyPojo> createpost(@Body MyPojo myPojo);

@FormUrlEncoded
@POST("posts")
Call<MyPojo> createPost(@Field("userId") int postid,@Field("title") String title,
@Field("body") String body);

@FormUrlEncoded
@POST("posts")
Call<MyPojo> createpostMap(@FieldMap Map<String,String> map);}
//create the Client Type Instance which is help you to debug the app
public class RestClient {

public OkHttpClient getClient() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();


return client;
}
}


Call the api from Mian Activity
   private void createPostMap() {
        Map<String,String> map=new HashMap<>();

map.put("userId","2");
map.put("title","My map post title");
map.put("body","mypost map body");
mMyService=MyService.retrifit.create(MyService.class);
Call<MyPojo> myPojoCall=mMyService.createpostMap(map);

myPojoCall.enqueue(new Callback<MyPojo>() {
@Override
public void onResponse(Call<MyPojo> call, Response<MyPojo> response) {
if(response.isSuccessful())
{
showPost(response.body());
}
}

@Override
public void onFailure(Call<MyPojo> call, Throwable t) {

}
});
}

private void createpostFromEncode() {

mMyService=MyService.retrifit.create(MyService.class);
Call<MyPojo> myPojoCall=mMyService.createPost(6,"my Title from encode","My Body encode");

myPojoCall.enqueue(new Callback<MyPojo>() {
@Override
public void onResponse(Call<MyPojo> call, Response<MyPojo> response) {
if(response.isSuccessful())
{
showPost(response.body());
}
}

@Override
public void onFailure(Call<MyPojo> call, Throwable t) {

}
});

}

private void crateAPost() {
MyPojo myPojo=new MyPojo(4,"my Title","My Body");
mMyService=MyService.retrifit.create(MyService.class);
Call<MyPojo> myPojoCall=mMyService.createpost(myPojo);

myPojoCall.enqueue(new Callback<MyPojo>() {
@Override
public void onResponse(Call<MyPojo> call, Response<MyPojo> response) {
if(response.isSuccessful())
{
// Log.d("mydata", "statuscode"+response.message();
showPost(response.body());
}

}

@Override
public void onFailure(Call<MyPojo> call, Throwable t) {

}
});


}


private void getPost() {
mMyService=MyService.retrifit.create(MyService.class);

Call<List<MyPojo>> call=mMyService.getPosts();

call.enqueue(new Callback<List<MyPojo>>() {
@Override
public void onResponse(Call<List<MyPojo>> call, Response<List<MyPojo>> response) {
if(response.isSuccessful())
{
Toast.makeText(MainActivity.this, "code run", Toast.LENGTH_SHORT).show();
for(MyPojo myPojo:response.body())
{
showPost(myPojo);
}
}
}

@Override
public void onFailure(Call<List<MyPojo>> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage()+"", Toast.LENGTH_SHORT).show();
Log.d("mytag","error"+t.getMessage());

}
});

}



private void getComment()
{
mMyService=MyService.retrifit.create(MyService.class);
Call<List<MyComment>> comment=mMyService.getComent();

comment.enqueue(new Callback<List<MyComment>>() {
@Override
public void onResponse(Call<List<MyComment>> call, Response<List<MyComment>> response) {

if(response.isSuccessful())
{
for (MyComment myComment:response.body())
{
showComment(myComment);
}
}else {
Toast.makeText(MainActivity.this, "task is not done", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFailure(Call<List<MyComment>> call, Throwable t) {
Log.d("mytag","error"+t.getMessage());

}
});
}

private void getComment2() {
mMyService=MyService.retrifit.create(MyService.class);

Call<List<MyComment>> comment=mMyService.getComment2(3);
comment.enqueue(new Callback<List<MyComment>>() {
@Override
public void onResponse(Call<List<MyComment>> call, Response<List<MyComment>> response) {
if(response.isSuccessful())
{
for (MyComment myComment:response.body())
{
showComment(myComment);
}
}
}

@Override
public void onFailure(Call<List<MyComment>> call, Throwable t) {

}
});

}

private void getComment3() {
mMyService=MyService.retrifit.create(MyService.class);
Map<String,String> map=new HashMap<>();

map.put("postId","5");
map.put("_sort","name");
map.put("_order","desc");
// Call<List<MyComment>> mycomment=mMyService.getComment3(new Integer[]{3,4,6},"email","desc");
Call<List<MyComment>> mycomment=mMyService.getComment3(map);
mycomment.enqueue(new Callback<List<MyComment>>() {
@Override
public void onResponse(Call<List<MyComment>> call, Response<List<MyComment>> response) {
if(response.isSuccessful())
{
for (MyComment comment:response.body())
{
showComment(comment);

}
}
}

@Override
public void onFailure(Call<List<MyComment>> call, Throwable t) {

}
});

}


private void showComment(MyComment myComment) {
mActivityMainBinding.showResult.append("postid : "+myComment.getPostId()+"\n");
mActivityMainBinding.showResult.append("name : "+myComment.getName()+"\n");
mActivityMainBinding.showResult.append("id : "+myComment.getId()+"\n");
mActivityMainBinding.showResult.append("Title : "+myComment.getEmail()+"\n");
mActivityMainBinding.showResult.append("Body : "+myComment.getBody()+"\n");
}
private void showPost(MyPojo myPojo) {
mActivityMainBinding.showResult.append("userId : "+myPojo.getUserId()+"\n");
// mActivityMainBinding.showResult.append("id : "+myPojo.getId()+"\n");
mActivityMainBinding.showResult.append("Title : "+myPojo.getTitle()+"\n");
mActivityMainBinding.showResult.append("Body : "+myPojo.getBody()+"\n");
}

No comments:

Post a Comment