Ps.:JSON数据未在CardView
中解析。
我使用片段查看每个项目点击的数据,这些都是通过滑块菜单识别的。我编写了使用Retrofit Web服务解析JSON数据的代码。
public class Physical_Geography_Activity extends Fragment{
View viewOne;
private RecyclerView recyclerView;
private ArrayList<QAModel> dataArray;
private DataAdapter adapter;
private ProgressDialog dialog;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
viewOne=inflater.inflate(R.layout.geo_physical_layout,container,false);
recyclerView=(RecyclerView)viewOne.findViewById(R.id.card_recycler_view);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(viewOne.getContext());
recyclerView.setLayoutManager(layoutManager);
loadJSON();
return viewOne;
}
private void loadJSON() {
dialog = ProgressDialog.show(getContext(),"Please wait","Loading..",true);
dialog.show();
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.myjson.com").addConverterFactory(GsonConverterFactory.create()).build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
Call<JSONResponse> call = requestInterface.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
dialog.dismiss();
JSONResponse jsonResponse=response.body();
dataArray = new ArrayList<QAModel>(Arrays.asList(jsonResponse.getPhysiography()));
adapter= new DataAdapter(dataArray);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
}
}
自定义ListView
查看数据并编写持有者类来保存数据,我使用了两个TextViews
来查看文本,即问答。每当我在远程服务器中添加数据时,问题和答案都会动态变化。
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private ArrayList<QAModel> arrayList;
public DataAdapter(ArrayList<QAModel> arrayList) {
this.arrayList = arrayList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.physical_card_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.question.setText(arrayList.get(position).getQuestion());
holder.answer.setText(arrayList.get(position).getAnswer());
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView question,answer;
public ViewHolder(View itemView) {
super(itemView);
question=(TextView)itemView.findViewById(R.id.tv_question);
answer=(TextView)itemView.findViewById(R.id.tv_answer);
}
}
}
Model有一个构造函数和两个私有字符串变量。我已经创建了用于从远程服务器获取和设置JSON数据的setter和getters方法。
public class QAModel {
private String Question;
private String Answer;
public QAModel(String question, String answer) {
Question = question;
Answer = answer;
}
public String getQuestion() {
return Question;
}
public void setQuestion(String question) {
Question = question;
}
public String getAnswer() {
return Answer;
}
public void setAnswer(String answer) {
Answer = answer;
}
}
编写JSON Response类是为了通过方法调用获取模型类的响应。
public class JSONResponse {
private QAModel[] physiography;
public QAModel[] getPhysiography()
{
return physiography;
}
}
Interface有一种从服务器获取数据的方法,即getJSON
,接口确实有后缀url,用于保存JSON数据。
public interface RequestInterface {
@GET("bins/lo1md")
Call<JSONResponse> getJSON();
}
答案 0 :(得分:0)
制作adapter.notifyDataSetChanged();
已编辑:
public class JSONResponse {
@SerializedName("physiography")
public List<Physiography> physiography = new ArrayList<Physiography>;
}
public class Physiography {
@SerializedName("answer")
public String answer;
@SerializedName("question")
public String question;
}
更改你的响应类,然后检查它
答案 1 :(得分:0)
您在基本网址“https://api.myjson.com”
中缺少“/”请更新您的专线
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.myjson.com").addConverterFactory(GsonConverterFactory.create()).build();
到这个
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.myjson.com/").addConverterFactory(GsonConverterFactory.create()).build();
您还需要将模型类更改为如下所示
public class JSONResponse {
@SerializedName("physiography")
@Expose
private List<Physiography> physiography = null;
public List<Physiography> getPhysiography() {
return physiography;
}
public void setPhysiography(List<Physiography> physiography) {
this.physiography = physiography;
}
}
和
public class Physiography {
@SerializedName("answer")
@Expose
private String answer;
@SerializedName("question")
@Expose
private String question;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
答案 2 :(得分:0)
首先,不要将您的响应对象命名为常见的(JSONResponse
)。请更恰当地命名,然后说PhysiographyResponse
。使用squareup中的Moshi
库,它会生成JSON
响应的JAVA对象。
Moshi依赖
implementation 'com.squareup.retrofit2:converter-moshi:2.3.0'
implementation 'com.squareup.moshi:moshi:1.5.0'
数据模型类 -
import com.squareup.moshi.Json;
public class PhysiographyResponse {
@Json(name = "physiography")
List<QAModel> QAModel;
public List<QAModel> getQAModel() {
return QAModel;
}
public void setQAModel(List<QAModel> QAModel) {
this.QAModel = QAModel;
}
}
import com.squareup.moshi.Json;
public class QAModel {
@Json(name = "answer")
String answer;
@Json(name = "question")
String question;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
Api界面
public interface RequestInterface {
@GET("bins/lo1md")
Call<PhysiographyResponse> getPhysiographyResponse();
}
改装电话
Call<PhysiographyResponse> call = requestInterface.getPhysiographyResponse();
call.enqueue(new Callback<PhysiographyResponse>() {
@Override
public void onResponse(Call<PhysiographyResponse> call, Response<PhysiographyResponse> response) {
dialog.dismiss();
dataArray = new ArrayList<QAModel>(Arrays.asList(resposne.getQAModel));
adapter= new DataAdapter(dataArray);
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("Error",t.getMessage());
}
});
答案 3 :(得分:0)
轰!
1)将此gradle放入您的项目中。
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.google.code.gson:gson:2.8.0'
2)制作2个模型,第1个用于
QAModel
public class QAModel{
@SerializedName("answer")
private String Answer;
@SerializedName("question")
private String Question;
public QAModel(String question, String answer) {
Question = question;
Answer = answer;
}
public String getQuestion() {
return Question;
}
public void setQuestion(String question) {
Question = question;
}
public String getAnswer() {
return Answer;
}
public void setAnswer(String answer) {
Answer = answer;
}
}
和第二个服务器的响应
ResponseQAModel
public class ResponseQAModel {
@SerializedName("physiography")
private List<QAModel> qaModels;
public List<QAModel> getQaModels() {
return qaModels;
}
public void setQaModels(List<QAModel> qaModels) {
this.qaModels = qaModels;
}
}
3)您设置改造的ApiClient
public class ApiClient {
public static final String BASE_URL = "https://api.myjson.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
4)您的路线ApiInterface
public interface ApiInterface {
@GET("/bins/lo1md")
Call<ResponseQAModel> getJSON();
}
5)现在是时候捕捉输出了;)
private void loadGSON() {
final Call<ResponseQAModel> responseQAModelCall;
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
responseQAModelCall = apiService.getJSON();
responseQAModelCall.enqueue(new Callback<ResponseQAModel>() {
@Override
public void onResponse(Call<ResponseQAModel> call, Response<ResponseQAModel> response) {
Log.d("kkkkk",response.body().getQaModels().toString());
//responseQAMODELS contains all response pass to your adapter
List<QAModel> responseQAModels = response.body().getQaModels();
}
@Override
public void onFailure(Call<ResponseQAModel> call, Throwable t) {
}
});
}