Search code examples
javaandroidretrofit2fastadapter

Using has method in the retrofit


Hi friends i cant able to understand how to do this method please help me out. i am using the retrofit for the Json parsing its working well when the data is there but when there is no data in it then the response will be different like below where when i use the Volley then i used has method to know that data is there or not but i cant do that in the retrofit

{
    "messae": "No Friends Found",
    "status": "failure"
}

But When data is there the response will be like this

{
    "friends": [
        {
            "username": "tester",
            "user_id": "7",
            "profile": "http://54.202.3.127/parallel_space/uploads/noimage.png",
            "city": "kakinada",
            "phone": "7894561231",
            "status": "1"
        },
        {
            "username": "tester",
            "user_id": "9",
            "profile": "http://54.202.3.127/parallel_space/uploads/http://54.202.3.127/parallel_space/uploads/pic-91488861905.jpg",
            "city": "kakinada",
            "phone": "7894561230",
            "status": "1"
        }
    ],
    "status": "Success"
}

Main.class

public class Friend_request extends AppCompatActivity {
    @BindView(R.id.frd_req_list)
    RecyclerView frd_req_list;
    FastItemAdapter<Friend_Request_adapter> friend_request_adapter = new FastItemAdapter<>();
    @BindView(R.id.frd_req_toolbar)
    Toolbar frd_req_toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_requets);
        ButterKnife.bind(this);
        setSupportActionBar(frd_req_toolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle("Friend Request");
        getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        frd_req_list.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
        frd_req_list.addItemDecoration(new Divider(this, LinearLayoutManager.VERTICAL));
        frd_req_list.setAdapter(friend_request_adapter);
        frdreq_method();
    }

    private void frdreq_method() {
        Constant.showloader(Friend_request.this);
        Retrofit frq_reqretrofit = new Retrofit.Builder().baseUrl(Constant.url).client(Constant.okClient()).addConverterFactory(GsonConverterFactory.create()).build();
        Api fre_req_api = frq_reqretrofit.create(Api.class);
        Call<Friend_Request_adapter> frq_req_call = fre_req_api.frdreqcall(Session.getUserID(getApplicationContext()));
        frq_req_call.enqueue(new Callback<Friend_Request_adapter>() {
            @Override
            public void onResponse(Call<Friend_Request_adapter> call, Response<Friend_Request_adapter> response) {
                Constant.l(String.valueOf(response));
                List<Friend_Request_adapter> frdlistadapter = response.body().getFriends();
                friend_request_adapter.add(frdlistadapter);
                Constant.dismissloader();
            }

            @Override
            public void onFailure(Call<Friend_Request_adapter> call, Throwable t) {
                Constant.l(t.toString());
                Constant.dismissloader();
            }
        });
    }

}

Adapter.class(Using mike penz fast adapter)

public class Friend_Request_adapter extends AbstractItem<Friend_Request_adapter, Friend_Request_adapter.FriendRequest_ViewHolder> {
    JsonObject frdreqbj;
    @SerializedName("messae")
    private String messae;
    @SerializedName("status")
    private String responsestatus;
    @SerializedName("friends")
    List<Friend_Request_adapter> friends;
    @SerializedName("profile")
    private String friendreq_pic;
    @SerializedName("username")
    private String frienduser_name;
    @SerializedName("status")
    private String friendstatus;
    @SerializedName("user_id")
    private String frduserid;

    public String getMessae() {
        return messae;
    }

    public void setMessae(String messae) {
        this.messae = messae;
    }

    public String getResponsestatus() {
        return responsestatus;
    }

    public void setResponsestatus(String responsestatus) {
        this.responsestatus = responsestatus;
    }

    public JsonObject getFrdreqbj() {
        return frdreqbj;
    }

    public void setFrdreqbj(JsonObject frdreqbj) {
        this.frdreqbj = frdreqbj;
    }

    public List<Friend_Request_adapter> getFriends() {
        return friends;
    }

    public void setFriends(List<Friend_Request_adapter> friends) {
        this.friends = friends;
    }

    public String getFrduserid() {
        return frduserid;
    }

    public void setFrduserid(String frduserid) {
        this.frduserid = frduserid;
    }

    public String getFriendstatus() {
        return friendstatus;
    }

    public void setFriendstatus(String friendstatus) {
        this.friendstatus = friendstatus;
    }

    public String getFriendreq_pic() {
        return friendreq_pic;
    }

    public void setFriendreq_pic(String friendreq_pic) {
        this.friendreq_pic = friendreq_pic;
    }

    public String getFrienduser_name() {
        return frienduser_name;
    }

    public void setFrienduser_name(String frienduser_name) {
        this.frienduser_name = frienduser_name;
    }

    @Override
    public int getType() {
        return R.id.frd_req_list;
    }


    @Override
    public int getLayoutRes() {
        return R.layout.friend_request_item;
    }

    @Override
    public void bindView(FriendRequest_ViewHolder holder, List<Object> payloads) {
        super.bindView(holder, payloads);
        holder.frd_req_item_name.setText(frienduser_name);
        holder.frd_req_msg.setText("Online");
        Picasso.with(holder.itemView.getContext()).load(friendreq_pic).into(holder.frd_req_img);
    }

    public static class FriendRequest_ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.frd_req_img)
        CircleImageView frd_req_img;
        @BindView(R.id.frd_req_item_name)
        TextView frd_req_item_name;
        @BindView(R.id.frd_req_msg)
        TextView frd_req_msg;
        @BindView(R.id.accept)
        FancyButton accept;
        @BindView(R.id.delete)
        FancyButton delete;


        public FriendRequest_ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

Solution

  • You have to check either success or failure from server. So you have to make another Model class and then check success or not

    for e.g.

    public class FriendsResposne implements Serializable {
        @SerializedName("status")
        private String status;
        @SerializedName("messae")
        private String message;
        @SerializedName("friends")
        private List< Friend_Request_adapter > friendsArr;
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status = status;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public List<Friend_Request_adapter> getFriendsArr() {
            return friendsArr;
        }
    
        public void setFriendsArr(List<Friend_Request_adapter> friendsArr) {
            this.friendsArr = friendsArr;
        }
    }
    

    and then call like

    private void frdreq_method() {
            Constant.showloader(Friend_request.this);
            Retrofit frq_reqretrofit = new Retrofit.Builder().baseUrl(Constant.url).client(Constant.okClient()).addConverterFactory(GsonConverterFactory.create()).build();
            Api fre_req_api = frq_reqretrofit.create(Api.class);
            Call<FriendsResposne> frq_req_call = fre_req_api.frdreqcall(Session.getUserID(getApplicationContext()));
            frq_req_call.enqueue(new Callback<FriendsResposne>() {
                @Override
                public void onResponse(Call<FriendsResposne> call, Response<FriendsResposne> response) {
                    Constant.l(String.valueOf(response));
    
                    FriendsResposne fr = response.body();
    
                    if(fr.getStatus.equals("Success")){// check here success or not
                         List<Friend_Request_adapter> frdlistadapter =     fr.getFriends();
                         friend_request_adapter.add(frdlistadapter);
                    }else{
                        Toast.makeText(this, fr.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                    Constant.dismissloader();
                }
    
                @Override
                public void onFailure(Call<FriendsResposne> call, Throwable t) {
                    Constant.l(t.toString());
                    Constant.dismissloader();
                }
            });
        }
    

    Let me know if you are facing any issue.