Android 改装2+;获取JSONArray时发生Gson非法状态异常
我最近刚开始使用Regulation+Gson,虽然我四处寻找问题的答案并尝试了几种解决方案,但似乎没有任何效果,结果是: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException: 应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$ 首先,返回的JSONArray如下所示:Android 改装2+;获取JSONArray时发生Gson非法状态异常,android,gson,retrofit2,Android,Gson,Retrofit2,我最近刚开始使用Regulation+Gson,虽然我四处寻找问题的答案并尝试了几种解决方案,但似乎没有任何效果,结果是: com.google.gson.JsonSyntaxException:java.lang.IllegalStateException: 应为BEGIN_对象,但在第1行第2列路径处为BEGIN_数组$ 首先,返回的JSONArray如下所示: [{ "id": 23545, "title": "some title", "description":
[{
"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"
}]
}]
}
}]
这是我的改装客户…
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 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());
}
});
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'
这是接口…
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 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());
}
});
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'
以及我发出请求的片段代码…
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 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());
}
});
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。我确信我的错误是显而易见的,但我没有看到或理解它。提前感谢您的帮助 按以下方式更改代码,它应该可以工作:
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());
}
});
Call Call=apinterface.getevaluations(cbi.onGetCookie());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
reviews=response.body();
//传递到RecyclerView适配器
}否则{
//处理失败
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(TAG,t.toString());
}
});
我对Review[]进行了更改,因为在您的回复中没有包含一系列评论的关键“评论”
在界面中尝试
Call<Review> getReviews(@Header("Cookie") String cookie);
调用getReviews(@Header(“Cookie”)字符串Cookie);
及
Call Call=apinterface.getReviews(cbi.onGetCookie());
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
String id=response.body().getId();
//传递到RecyclerView适配器
}否则{
//处理失败
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(TAG,t.toString());
}
}))
Call
应该是Call
。剩下的似乎fine@Blackbelt谢谢,实际上我已经试过了,但也许我忽略了什么。“我再试试看。”布莱克贝尔谢谢,你的建议和布莱克贝尔的回答同样有效。我以前试过两种方法,但我的错误是在复习课上,你们两个让我意识到这是错误的一部分。干杯谢谢Arpit,你的解决方案和Blackbelt的建议都有效。我的一部分错误是使用了评论模式,所以尝试了,但从未成功。。。。现在我明白了;)。干杯