Retrofit如何构建响应类以从列表嵌套对象json中获取数据

时间:2014-10-25 01:27:24

标签: android json

我正在使用改造来从nodejs(作为API)获取响应json到我的Android应用程序。 它工作正常,直到我得到这个嵌套的列表对象json。

{
    "Profiles": [
    {
        "Firstname": "Alex",
        "Lastname": "Ho",
        "id": "5448cc75ced7cc8c050f4f0d"
    },
    {
        "Firstname": "Eli",
        "Lastname": "Tran",
        "id": "54491e5c5c3624c816d1ecd4"
    }], 
    "Status": "Success" 
}

我已经尝试过一些方法来构建这样的对象响应,但它不起作用。

ListFriendProfileResponse.java

public class ListFriendProfileResponse {
    @SerializedName("Profiles")
    @Expose
    private List<Profile> profiles = new ArrayList<Profile>();

    @SerializedName("Status")
    @Expose
    private String status;

    public List<Profile> getProfiles() {
        return profiles;
    }
}

Profile.java

public class Profile {
    @SerializedName("Firstname")
    @Expose
    private String firstname;

    @SerializedName("Lastname")
    @Expose
    private String lastname;
    // ... 
}

进行改造以获取数据

@POST("/profile/getAll")
void userGetAllProfile(@Body Object body, Callback<ListFriendProfileResponse> callback);

@POST("/profile/getAll")
void userGetAllProfile(@Body Object body, Callback<List<Profile>> callback);

问题是json响应不适合我的java对象。有人可以告诉我。感谢

1 个答案:

答案 0 :(得分:0)

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

接口类:



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课程如下:



    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