我在Retrofit中完全是初学者。我正在尝试使用改装注释来制作以下响应的对象。
回复:
{"status":"1",
"data":[{"title":"hb","rate":"12",
"pic":[{"file_name":"x"},{"file_name":"y"}]}
{"title":"hk","rate":"15",
"pic":[{"file_name":"x"},{"file_name":"y"}]}
{"title":"hc","rate":"18",
"pic":[{"file_name":"x"},{"file_name":"y"}]}
]
}
为此,我编写了代码以在recycler视图中显示它。 通过编写以下代码,我能够获取数据对象的元素,但无法访问响应的pic对象的元素。 我应该在这段代码中编辑什么来访问pic对象的元素?
Book_Hotel.java:
public class Book_Hotel extends Fragment {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private Recycler_Adapter adapter;
private Api_Contact con;
private Api_Interface apiInterface;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_book__hotel, container, false);
getActivity().setTitle("Room List");
recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
apiInterface = Api_Client.getApiClient().create(Api_Interface.class);
Call<Api_Contact> call = apiInterface.getContacts();
call.enqueue(new Callback<Api_Contact>() {
@Override
public void onResponse(Call<Api_Contact> call, Response<Api_Contact> response) {
con = response.body();
Log.e("data", con.getData().toString());
adapter = new Recycler_Adapter(getActivity(), con.getData());
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<Api_Contact> call, Throwable t) {
Log.e("fail", t.toString());
if (t.toString().contains(" Unable to resolve")) {
Toast.makeText(getActivity(), "Internet connect prob", Toast.LENGTH_SHORT).show();
}
}
});
return view;
}
}
的 Api_Client.java:
public class Api_Client {
public static final String BASE_URL="xxxxxxxxxxxxxxxxxxxxxxxxxxx";
public static Retrofit retrofit = null;
public static Retrofit getApiClient(){
if(retrofit==null){
retrofit=new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
Log.e("in client",retrofit.toString());
}
return retrofit;
}
}
Api_Contact.java:
public class Api_Contact {
@SerializedName("status")
private String Status;
@SerializedName("data")
private List<data> Data;
public String getStatus() {
return Status;
}
public List<data> getData() {
Log.e("in get data of contact", Data.toString());
return Data;
}
}
Api_Interface.java:
public interface Api_Interface{
@GET("xxxxxxx")
Call<Api_Contact> getContacts();
}
Recycler_Adapter.java:
public class Recycler_Adapter extends RecyclerView.Adapter<Recycler_Adapter.MyViewHolder> {
private List<data> data;
Context context;
public Recycler_Adapter(Context context, List<data> contacts) {
this.context = context;
this.data = contacts;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.roomlist_row, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(Recycler_Adapter.MyViewHolder holder, int position) {
holder.title.setText(data.get(position).getTitle());
holder.price.setText(data.get(position).getRate());
}
@Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView title, price;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.awesom);
price = (TextView) itemView.findViewById(R.id.rate);
}
}
}
请帮我解决这个问题。我们将不胜感激。谢谢。
答案 0 :(得分:0)
你需要这样做:
public class Api_Contact {
@SerializedName("status")
private String status;
@SerializedName("data")
private List<Data> data;
public String getStatus() {
return status;
}
public List<Data> getData() {
Log.e("in get data of contact", data.toString());
return data;
}
}
你需要创建另外两个类:
public class Data {
@SerializedName("title")
private String title;
@SerializedName("rate")
private String rate;
@SerializedName("pic")
private List<Pic> picList;
public void setTitle(String title) {
this.title = title;
}
public void setRate(String rate) {
this.rate = rate;
}
public void setPicList(List<Pic> picList) {
this.picList = picList;
}
public String getTitle() {
return title;
}
public String getRate() {
return rate;
}
public List<Pic> getPicList() {
return picList;
}
}
和
public class Pic {
@SerializedName("file_name")
private String fileName;
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
}