正在从改造中获取 json 对象和数组

Fetching json object and array from retrofit

我想从 [this link][1] 获取 json:https://api.myjson.com/bins/38ln5 使用 retrofit

样本 json 是

{
  "students": [
    {
      "id": "1",
      "name": "Larzobispa"
    },
    {
      "id": "2",
      "name": "La Cibeles"
    }
  ]
}

请详细说明如何操作。 非常感谢,伙计们!

Retrofit 会自动解析 JSON 对象以及 JSON 数组。

@GET("/register?email=example@123.com")
public void responseString(Callback<Student> response);

您的模型 class 看起来像:

public class Student{
private ArrayList<StudentInfo> studentList = new ArrayList<>();
     //getter and setters
}

public class StudentInfo{
    private String id;
    private String name;
    //getters and setters
}

然后回应:

@Override
public void onResponse(Response<Student> response, Retrofit retrofit) {
    if (response.isSuccess()) {
        Student student = response.body;
        Log.e("Student name", student.getStudent().get(0).getName()); // do whatever you want
    }else{
        // get response.errorBody()
    }
}

您可以在 android 中使用 retrofit 2.0 访问 JSONArray 和 JSONObject。在这里你想使用改造来访问 JSONArray,为此你应该执行以下操作:

接口Class:



package com.androidtutorialpoint.retrofitandroid;
import java.util.List;

import retrofit.Call;
import retrofit.http.GET;

/**
 * Created by navneet on 4/6/16.
 */
public interface RetrofitArrayAPI {

    /*
     * Retrofit get annotation with our URL
     * And our method that will return us details of student.
    */
    @GET("api/RetrofitAndroidArrayResponse")
    Call> getStudentDetails();

}


以上是界面。现在要在您的智能手机屏幕上显示 JSONArray 数据,请调用以下函数:



    void getRetrofitArray() {
    Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);

        Call> call = service.getStudentDetails();

        call.enqueue(new Callback>() {

         @Override
            public void onResponse(Response> response, Retrofit retrofit) {

              try {
                List StudentData = response.body();
                for (int i = 0; i<StudentData.size(); i++) {
                  if (i == 0) {
                            text_id_1.setText("StudentId  :  " + StudentData.get(i).getStudentId());
                            text_name_1.setText("StudentName  :  " + StudentData.get(i).getStudentName());
                            text_marks_1.setText("StudentMarks  : " + StudentData.get(i).getStudentMarks());
                  } else if (i == 1) {
                            text_id_2.setText("StudentId  :  " + StudentData.get(i).getStudentId());
                            text_name_2.setText("StudentName  :  " + StudentData.get(i).getStudentName());
                            text_marks_2.setText("StudentMarks  : " + StudentData.get(i).getStudentMarks());
                  }
                }
              } catch (Exception e) {
                    Log.d("onResponse", "There is an error");
                    e.printStackTrace();
              } 
           }

           @Override
           public void onFailure(Throwable t) {
                Log.d("onFailure", t.toString());
           }

        });
    }


我的 POJO Class 正在关注:



    package com.androidtutorialpoint.retrofitandroid;

public class Student {

    //Variables that are in our json
    private int StudentId;
    private String StudentName;
    private String StudentMarks;
    private int inStock;

    //Getters and setters
    public int getStudentId() {
        return StudentId;
    }

    public void setStudentId(int bookId) {
        this.StudentId = StudentId;
    }

    public String getStudentName() {
        return StudentName;
    }

    public void setStudentName(String name) {
        this.StudentName = StudentName;
    }

    public String getStudentMarks() {
        return StudentMarks;
    }

    public void setStudentMarks(String price) {
        this.StudentMarks = StudentMarks;
    }

}


因此,通过这种方式,您将能够使用 Retrofit 2.0 从 URL 捕获 JSONArray 并将该数据显示在屏幕上。

希望能够回答您的问题。

致谢:我阅读了本教程: AndroidTutorialPoint for Retrofit 2.0