[英]parsing nested json using retrofit in android?
如何使用看起来像这样的改造来解析 JSON 响应,任何帮助将不胜感激,现在已经坚持了一段时间。
更新:以下是我到目前为止尝试过的代码,但我无法获取数据,也无法弄清楚它有什么问题,jsonschema2pojo 给了我两个 Java 类,这就是我在代码中使用的. 根据我的理解,我应该使用这两个 java 类并传递我的 baseurl 和端点,然后在我的活动中调用它们。 我认为我已经正确地遵循了程序,但到目前为止还没有成功检索数据。
{
"status": "success",
"data": [
{
"id": "1",
"employee_name": "Tiger Nixon",
"employee_salary": "320800",
"employee_age": "61",
"profile_image": ""
},
....
]
}
主活动.java
private ArrayList<Datum> results;
private EmployeeAdapter employeeAdapter;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getEmployees();
}
private void getEmployees() {
GetEmployeeData getCountryDataService = RetrofitInstance.getService();
Call<Info> call = getCountryDataService.getResults();
call.enqueue(new Callback<Info>() {
@Override
public void onResponse(Call<Info> call, Response<Info> response) {
Info info = response.body();
if (info != null && info.getData() != null) {
results = (ArrayList<Datum>) info.getData();
viewData();
}
}
@Override
public void onFailure(Call<Info> call, Throwable t) {
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
});
}
private void viewData() {
recyclerView = (RecyclerView) findViewById(R.id.rv_countries_list);
employeeAdapter = new EmployeeAdapter(results);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(employeeAdapter);
}
}
数据文件
@SerializedName("id")
@Expose
private String id;
@SerializedName("employee_name")
@Expose
private String employeeName;
@SerializedName("employee_salary")
@Expose
private String employeeSalary;
@SerializedName("employee_age")
@Expose
private String employeeAge;
@SerializedName("profile_image")
@Expose
private String profileImage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(String employeeSalary) {
this.employeeSalary = employeeSalary;
}
public String getEmployeeAge() {
return employeeAge;
}
public void setEmployeeAge(String employeeAge) {
this.employeeAge = employeeAge;
}
public String getProfileImage() {
return profileImage;
}
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
信息.java
@SerializedName("status")
@Expose
private String status;
@SerializedName("data")
@Expose
private List<Datum> data = null;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
然后我使用的界面是这个
获取员工数据.java
@GET("employees")
Call<Info> getResults();
}
改造实例.java
私有静态改造改造= null; 私有静态字符串 BASE_URL = " http://dummy.restapiexample.com/api/v1/ ";
public static GetEmployeeData getService() {
if (retrofit == null) {
retrofit = new Retrofit
.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit.create(GetEmployeeData.class);
}
这是一个如何创建模型类的示例。 我是在Kotlin
,但我相信您可以轻松地将其转换为Java
。
import com.google.gson.annotations.SerializedName
data class EmployeeDTO(
@SerializedName("id")
var id: Int,
@SerializedName("employee_name")
var name: String,
@SerializedName("employee_salary")
var salary: Int,
@SerializedName("employee_age")
var age: Int,
@SerializedName("profile_image")
var image: Boolean
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.