Java has方法在改造中的应用

Java has方法在改造中的应用,java,android,retrofit2,fastadapter,Java,Android,Retrofit2,Fastadapter,嗨,朋友们,我不能理解如何做这个方法,请帮助我。我正在使用Json解析的改进,当数据存在时,它工作得很好,但当其中没有数据时,响应将不同,如下所示,当我使用截击时,我使用has方法来知道数据是否存在,但我不能在改进中这样做 { "messae": "No Friends Found", "status": "failure" } 但当数据存在时,响应将是这样的 { "friends": [ { "username": "teste

嗨,朋友们,我不能理解如何做这个方法,请帮助我。我正在使用Json解析的改进,当数据存在时,它工作得很好,但当其中没有数据时,响应将不同,如下所示,当我使用截击时,我使用has方法来知道数据是否存在,但我不能在改进中这样做

{
    "messae": "No Friends Found",
    "status": "failure"
}
但当数据存在时,响应将是这样的

{
    "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"
}
主类

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();
            }
        });
    }

}
public-class-Friend\u请求扩展了AppCompative活动{
@BindView(R.id.frd需求列表)
RecyclerView frd_需求列表;
FastItemAdapter friend_request_adapter=新建FastItemAdapter();
@BindView(R.id.frd\U req\U工具栏)
工具栏frd_req_工具栏;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.friend_请求);
把(这个)绑起来;
设置支持操作栏(frd\U req\U工具栏);
断言getSupportActionBar()!=null;
getSupportActionBar().setTitle(“好友请求”);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
frd_req_list.setLayoutManager(新的LinearLayoutManager(getApplicationContext(),LinearLayoutManager.VERTICAL,false));
frd_REQUE_list.ADDITEMDECRATION(新分隔符(此为LinearLayoutManager.VERTICAL));
设置适配器(朋友请求适配器);
frdreq_方法();
}
私有void frdreq_方法(){
常量.showloader(Friend_request.this);
Reformation frq_reqReformation=new reformation.Builder().baseUrl(Constant.url).client(Constant.okClient()).addConverterFactory(GsonConverterFactory.create()).build();
Api fre_req_Api=frq_req REQUANTIONG.create(Api.class);
调用frq_req_Call=fre_req_api.frdreqcall(Session.getUserID(getApplicationContext());
frq_req_call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
常量.l(字符串.valueOf(响应));
List frdlistadapter=response.body().getFriends();
好友请求适配器。添加(frdlistadapter);
常量。dismissloader();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
常数.l(t.toString());
常量。dismissloader();
}
});
}
}
类(使用mike penz快速适配器)

公共类好友请求适配器扩展了AbstractItem{
JsonObject frdreqbj;
@序列化名称(“messae”)
私有字符串消息;
@序列化名称(“状态”)
私有字符串响应状态;
@序列化名称(“朋友”)
列出朋友;
@序列化名称(“配置文件”)
私有字符串friendreq_pic;
@序列化名称(“用户名”)
私有字符串frienduser\u name;
@序列化名称(“状态”)
私有字符串状态;
@SerializedName(“用户id”)
私有字符串frduserid;
公共字符串getMessae(){
返回消息;
}
公共无效设置消息(字符串消息){
this.messae=messae;
}
公共字符串getResponsestatus(){
返回响应状态;
}
公共无效setResponsestatus(字符串responsestatus){
this.responsestatus=responsestatus;
}
公共JsonObject getFrdreqbj(){
返回frdreqbj;
}
公共无效集合frdreqbj(JsonObject frdreqbj){
this.frdreqbj=frdreqbj;
}
公共列表getFriends(){
回报朋友;
}
公众朋友(列出朋友){
这个。朋友=朋友;
}
公共字符串getFrduserid(){
返回frduserid;
}
公共void setFrduserid(字符串frduserid){
this.frduserid=frduserid;
}
公共字符串getFriendstatus(){
返回好友状态;
}
public void setFriendstatus(字符串friendstatus){
this.friendstatus=friendstatus;
}
公共字符串getFriendreq_pic(){
返回friendreq_pic;
}
public void setFriendreq_pic(字符串friendreq_pic){
this.friendreq_pic=friendreq_pic;
}
公共字符串getFrienduser_name(){
返回frienduser\u名称;
}
public void setFrienduser\u name(字符串frienduser\u name){
this.frienduser\u name=frienduser\u name;
}
@凌驾
公共int getType(){
返回R.id.frd需求列表;
}
@凌驾
公共int getLayoutRes(){
返回R.layout.friend\u请求\u项;
}
@凌驾
public void bindView(FriendRequest\u视图持有者,列表有效负载){
超级bindView(支架、有效载荷);
holder.frd\u req\u item\u name.setText(frienduser\u name);
持有人frd_REQUE_msg.SETEXT(“在线”);
Picasso.with(holder.itemView.getContext()).load(friendreq\u pic).into(holder.frd\u req\u img);
}
公共静态类FriendRequest\u ViewHolder扩展了RecyclerView.ViewHolder{
@BindView(R.id.frd\U req\U img)
CircleImageView frd\U REQU\U img;
@BindView(R.id.frd\U需求项目名称)
TextView frd_REQUE_项目名称;
@BindView(R.id.frd\U req\U msg)
TextView frd_req_msg;
@BindView(R.id.accept)
幻想按钮接受;
@BindView(R.id.delete)
FancyButton删除;
公共FriendRequest_视图持有者(视图项视图){
超级(项目视图);
ButterKnife.bind(这个,itemView);
}
}
}

您必须从服务器上检查
成功
失败
。因此,您必须创建另一个模型类,然后检查成功与否

例如

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;
    }
}
public-class-FriendsResposne实现可序列化{
@序列化名称(“状态”)
私有字符串状态;
@序列化名称(“messae”)
私有字符串消息;
@序列化名称(“朋友”)
私有列表friendsar;
公共字符串getStatus(){
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;
    }
}
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();
            }
        });
    }