使用Retrofit 2.1从JSON获取列表

时间:2016-07-13 09:01:41

标签: java android json retrofit2

我正在尝试获取ArrayList<Manager>并使用Retrofit 2.1将其解析为GET

我创建了一个带有response.isSuccessful()请求的界面,并初始化了Retrofit。

为什么 { "managers": [ {"name": "John Smith", "plan": "100"}, {"name": "Alex Sprite", "plan": "96"}, {"name": "Den Bollom", "plan": "2"}, {"name": "Jaromir Jagr", "plan": "68"}, {"name": "Wane Gretzky", "plan": "99"}, {"name": "Winsent Trade", "plan": "54"}, {"name": "Paul Dagger", "plan": "30"}, {"name": "Tommy Gun", "plan": "60"}, {"name": "Luc Perry", "plan": "15"}, {"name": "Tend Smarr", "plan": "64"} ] } 是假的?

JSON:

public interface ManagerAPI {


    String URL = "https://api.myjson.com/bins/get/";



    @GET("xkyz.json")
    Call <List<Manager>> getManagers();

}

Interface ManagerAPI:

public class Manager {

    @SerializedName("managers")
    private ArrayList<Manager> managers;


    public ArrayList<Manager> getManagers() {
        return managers;
    }

    public void setManagers(ArrayList<Manager> managers) {
        this.managers = managers;
    }


    public Manager() {

    }

    String name;
    int plan;


    public Manager(String name, int plan) {
        this.name = name;
        this.plan = plan;
    }


    public String getName()
    {
        return name;
    }

    public int getPlan() {
        return plan;
    }
}

POJO级:

private void requestData() {

    Retrofit retrofit = getRetrofit();
    ManagerAPI service = retrofit.create(ManagerAPI.class);
    Call<List<Manager>> call = service.getManagers();
    call.enqueue(new Callback<List<Manager>>() {
        @Override
        public void onResponse(Call<List<Manager>> call, Response<List<Manager>> response) {

            Log.e("OnResponce", "OK");
            if (response.isSuccessful()){
                List<Manager>  managers = response.body();
            }
            else Log.e("Response", "Not ok");
        }

        @Override
        public void onFailure(Call<List<Manager>> call, Throwable t) {
            Log.e("OnFailure", "Fail");
        }
    });

}

@NonNull
private Retrofit getRetrofit() {
    return new Retrofit.Builder()
                .baseUrl(ManagerAPI.URL)
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
}

InitRetrofit:

07-13 20:43:34.762 5402-5402/ru.alexbykov.jsonexample E/OnResponce: OK
07-13 20:43:34.762 5402-5402/ru.alexbykov.jsonexample E/Response Successful?: NO
07-13 20:43:34.762 5402-5402/ru.alexbykov.jsonexample E/response.message: Not Found
07-13 20:43:34.762 5402-5402/ru.alexbykov.jsonexample E/response.message: 404
07-13 20:43:34.762 5402-5402/ru.alexbykov.jsonexample E/response.message: null
07-13 20:43:34.978 5402-5469/ru.alexbykov.jsonexample E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa9fd21c0

LOG:

[x for x in politicianName if x in article and x is not'']

1 个答案:

答案 0 :(得分:2)

好的,步骤1,我会修改JSON响应以避免额外的复杂性。这意味着删除根元素,使其看起来如下所示。我们留下了一个简单明了的数组。

[
    {"name": "John Smith", "plan": "100"},
    {"name": "Alex Sprite", "plan": "96"},
    {"name": "Den Bollom", "plan": "2"},
    {"name": "Jaromir Jagr", "plan": "68"},
    {"name": "Wane Gretzky", "plan": "99"},
    {"name": "Winsent Trade", "plan": "54"},
    {"name": "Paul Dagger", "plan": "30"},
    {"name": "Tommy Gun", "plan": "60"},
    {"name": "Luc Perry", "plan": "15"},
    {"name": "Tend Smarr", "plan": "64"}
] 

步骤2清理您的POJO。 您可以调用类Manager,但是您可以在所述模型中包含有关包含管理器列表的方法。这些方法冗余​​和/或无意。把这些东西放在下面。

@SerializedName("managers")
private ArrayList<Manager> managers;


public ArrayList<Manager> getManagers() {
    return managers;
}

public void setManagers(ArrayList<Manager> managers) {
    this.managers = managers;
}