如何为 JSON 数组获取正确的 URL

How to get the correct URL for a JSON array

我在 JSONbin.io 中有一些具有以下结构的 JSON 对象:

{
  "employee_contributions": [
    {
      "transaction_id": "01",
      "staff_no": "12",
      "amount": "10000",
      "date": "20-2-2020"
   }
  ],
  "employer_contributions": [
    {
      "transaction_id": "01",
      "staff_no": "12",
      "amount": "10000",
      "date": "20-2-2020"
    }
  ]
}

我想访问 employee_contributions 数组中的对象,但出现 404 错误。我相信我的 URL 是问题所在,但我不知道为什么。这是相关的主要 activity 代码:

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.jsonbin.io/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        yeeAPI yeeapi = retrofit.create(yeeAPI.class);
        Call<List<yee_contributions>> call = yeeapi.getYee_Contributions();
        call.enqueue(new Callback<List<yee_contributions>>() {
            @Override
            public void onResponse(Call<List<yee_contributions>> call, Response<List<yee_contributions>> response) {
                if (!response.isSuccessful()) {
                    yee_contr.setText("code: " + response.code());
                    return;
                }
                List<yee_contributions> contrs = response.body();
                for (yee_contributions yee_contributions : contrs){
                    String content = "";
                    content+=yee_contributions.getTransaction_id()+" ";
                    content+=yee_contributions.getStaff_no()+" ";
                    content+=yee_contributions.getAmount()+" ";
                    content+=yee_contributions.getDate()+"\n\n";
                    yee_contr.append(content);
                }
            }

            @Override
            public void onFailure(Call<List<yee_contributions>> call, Throwable t) {
                yee_contr.setText(t.getMessage());

            }

        });

这是我的界面:

public interface yeeAPI {
    @GET("b/5e536928d3c2f35597f6ca46/3/employeee_contributions")
    Call<List<yee_contributions>> getYee_Contributions();
   
}

我的模型class:

public class yee_contributions {
    private String transaction_id;
    private String staff_no;
    private String amount;
    private String date;

    public String getTransaction_id() {
        return transaction_id;
    }

    public String getStaff_no() {
        return staff_no;
    }

    public String getAmount() {
        return amount;
    }

    public String getDate() {
        return date;
    }
}

基础 URL 没问题。只有当我将 /employee_contributions 添加到我的端点时,我才会收到 404 错误。

您无法通过这种方式访问​​ employeee_contributions。您必须先获取完整数据,然后从中解析 employeee_contributions。为此,您必须进行一些更改。检查以下内容:

步骤 - 1: 要使您的 json 响应与下面的另一个模型保持一致:

public class YeeResponse {
    private List<yee_contributions> employee_contributions;
    private List<yee_contributions> employer_contributions;

    //getter-setter

}

第 2 步: 更改您的 API 界面,将其合并如下:

public interface yeeAPI {
    @GET("b/5e536928d3c2f35597f6ca46/3/")
    Call<YeeResponse> getYee_Contributions();
}

步骤 - 3: 更改 enqueue 的实现并像下面这样解析数据:

Call<YeeResponse> call = yeeapi.getYee_Contributions();
call.enqueue(new Callback<YeeResponse>() {

    @Override
    public void onResponse(Call<YeeResponse> call, Response<YeeResponse> response) {
        if (!response.isSuccessful()) {

            return;
        }

        YeeResponse yeeResponse = response.body();
        List<yee_contributions> employee_contributions = yeeResponse.getEmployee_contributions();

        // Now loop through employee_contributions to get yee_contributions

        ....
    }

    @Override
    public void onFailure(Call<YeeResponse> call, Throwable t) {

        ....
    }

});