在改造中使用 has 方法
Using has method in the retrofit
嗨,朋友们,我无法理解如何执行此方法,请帮助我。我正在使用改造 Json 解析它在数据存在时运行良好但是当其中没有数据时响应将有所不同,如下所示,当我使用 Volley 然后我使用 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"
}
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(使用 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);
}
}
}
您必须从服务器检查 success
或 failure
。所以你必须制作另一个模型class然后检查是否成功
例如
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();
}
});
}
如果您遇到任何问题,请告诉我。
嗨,朋友们,我无法理解如何执行此方法,请帮助我。我正在使用改造 Json 解析它在数据存在时运行良好但是当其中没有数据时响应将有所不同,如下所示,当我使用 Volley 然后我使用 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"
}
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(使用 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);
}
}
}
您必须从服务器检查 success
或 failure
。所以你必须制作另一个模型class然后检查是否成功
例如
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();
}
});
}
如果您遇到任何问题,请告诉我。