【问题标题】:Using has method in the retrofit在改造中使用 has 方法
【发布时间】:2017-03-07 06:22:17
【问题描述】:

您好朋友,我无法理解如何执行此方法,请帮助我。我正在使用 Json 的改造来解析其在数据存在时运行良好,但是当其中没有数据时,响应会有所不同,如下所示,当我使用 Volley 时,我使用了知道数据存在的方法与否,但我不能在改造中做到这一点

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

}

Adapter.class(使用 mike penz 快速适配器)

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

【问题讨论】:

标签: java android retrofit2 fastadapter


【解决方案1】:

您必须从服务器检查successfailure。所以你必须制作另一个模型类,然后检查成功与否

例如

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

如果您遇到任何问题,请告诉我。

【讨论】:

  • @orienlalxoxoorienlalxoxo 是的,但是您知道课程不会占用太多内存。它以 KB 为单位。我们必须为每个响应创建类。
  • @orienlalxoxoorienlalxoxo 你找到解决方案了吗??
  • @orienlalxoxoorienlalxoxo 发生了什么?
  • 你面向哪里?
  • @orienlalxoxoorienlalxoxo 请订阅我的 youtube 频道以获取有关 iOS 和 Androd 的更多更新youtube.com/channel/UCA5HRmlkT7bqbO56EIlT0UQ
猜你喜欢
  • 2020-01-07
  • 2020-01-31
  • 2017-07-27
  • 2018-09-09
  • 1970-01-01
  • 2020-01-17
  • 2016-08-17
  • 1970-01-01
  • 2019-09-19
相关资源
最近更新 更多