获取JSONArray

时间:2016-06-16 11:21:00

标签: android gson retrofit2

我刚刚开始使用Retrofit + Gson,虽然我已经四处寻找我的问题的答案并尝试了几种解决方案,但似乎没有任何效果,而是导致:

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:   预期BEGIN_OBJECT但在第1行第2列路径$

处是BEGIN_ARRAY

首先,返回的JSONArray看起来像这样:

[{
    "id": 23545,
    "title": "some title",
    "description": "some description",
    "questions": {
        "text": [{
            "question": "Some question"
        }, {
            "question": "Some other question"
        }],
        "checkbox": [{
            "question": "Some question",
            "options": [{
                "value": "3",
                "correct": "1"
            }, {
                "value": "2",
                "correct": "0"
            }, {
                "value": "1",
                "correct": "0"
            }]
        }]
     }
}]

这是我的Retrofit客户端......

public class ApiClient {

    public static final String BASE_URL = "http://somewhere.com/";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        }
        return retrofit;
    }

    public static ApiInterface getApiInterface() {
        return getClient().create(ApiInterface.class);
    }

}

这是界面...

public interface ApiInterface {

    @Headers("Content-Type: application/x-www-form-urlencoded")
    @GET("rest/reviews.json")
    Call<Reviews> getReviews(@Header("Cookie") String cookie);

}

这是评论模型:

public class Reviews {

    private ArrayList<Review> reviews = new ArrayList<>();

    public ArrayList<Review> getReviews() {
        return reviews;
    }

    public void setReviews(ArrayList<Review> reviews) {
       this.reviews = reviews;
    }

}

评论模型......

public class Review {

    @SerializedName("id") private Integer id;
    @SerializedName("title") private String title;
    @SerializedName("description") private String description;
    @SerializedName("questions") private Questions questions;

    public String getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    etc...
}

我发出请求的片段代码......

ArrayList<Review> reviews = new ArrayList<>();

ApiInterface apiInterface = ApiClient.getApiInterface();

Call<Reviews> call = apiInterface.getReviews(cbi.onGetCookie());

call.enqueue(new Callback<Reviews>() {
    @Override
    public void onResponse(Call<Reviews> call, Response<Reviews> response) {
        if(response.isSuccessful()) {
            reviews = response.body().getReviews();
            // pass on to RecyclerView adapter
        } else {
            // handle fail
        }
    }
    @Override
    public void onFailure(Call<Reviews> call, Throwable t) {
        Log.e(TAG, t.toString());
    }
});

Gradle依赖关系以防任何人感兴趣...

compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

我理解错误信息,我只想弄清楚如何让Gson期待JSONArray。我确定我的错误是显而易见的,但我没有看到或理解它。在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

更改下面的代码,它应该有效:

Call<Review []> call = apiInterface.getAppraisals(cbi.onGetCookie());

call.enqueue(new Callback<Review []>() {
    @Override
    public void onResponse(Call<Review []> call, Response<Review[]> response) {
        if(response.isSuccessful()) {
            reviews = response.body();
            // pass on to RecyclerView adapter
        } else {
            // handle fail
        }
    }
    @Override
    public void onFailure(Call<Review[]> call, Throwable t) {
        Log.e(TAG, t.toString());
    }
});
  

我已将评论更改为评论[],因为在您的回复中没有关键字&#34;评论&#34;其中包含Review的数组。

答案 1 :(得分:0)

在界面中尝试

Call<Review> getReviews(@Header("Cookie") String cookie);

Call<Review> call = apiInterface.getReviews(cbi.onGetCookie());
call.enqueue(new Callback<Review>() {
@Override
public void onResponse(Call<Review> call, Response<Review> response) {
    if(response.isSuccessful()) {
        String id = response.body().getId();
        // pass on to RecyclerView adapter
    } else {
        // handle fail
    }
}
@Override
public void onFailure(Call<Reviews> call, Throwable t) {
    Log.e(TAG, t.toString());
}

});