【发布时间】:2017-08-27 06:21:07
【问题描述】:
如果我向服务器发送 ID、密码,它必须给我一个 json,但它给了我一个错误。
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为 BEGIN_OBJECT 但在第 1 行第 2 列路径为 BEGIN_ARRAY
这是改造代码:
public void postCheckUser(String id, String password) {
retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(url)
.build();
Map map = new HashMap();
map.put("id",id);
map.put("password", password);
webService = retrofit.create(WebService.class);
Call<PastMemo> call = webService.getPastMain(map);
call.enqueue(new Callback<PastMemo>() {
@Override
public void onResponse(Call<PastMemo> call, Response<PastMemo> response) {
if (response.code() == 200) {
title = response.body().getTitle();
letter= response.body().getLetter();
date = response.body().getDate();
Toast.makeText(getActivity(), "SignIn Success", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "SignIn Failed", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<PastMemo> call, Throwable t) {
Toast.makeText(getActivity(), "SignIn Denied", Toast.LENGTH_SHORT).show();
}
});
}
这是网络服务代码:
public interface WebService {
@FormUrlEncoded
@POST("PastMain")
Call<PastMemo> getPastMain(
@FieldMap Map<String, String> user
);
}
这是 PastMemo 代码
public class PastMemo {
@SerializedName("title")
@Expose
private String title;
@SerializedName("letter")
@Expose
private String letter;
@SerializedName("date")
@Expose
private String date;
public String getTitle() {
return title;
}
public String getLetter() {
return letter;
}
public String getDate() {
return date;
}
}
编辑: 这是我必须得到的json
[
{
"title": null,
"letter": null,
"date": null
},
{
"title": "a",
"letter": "b",
"date": "c"
}
]
【问题讨论】:
-
请发布它抛出相同异常的那一行
-
我调试了所有行以检查错误行,但它没有显示出来。它只适用于 onFailure() 方法。
标签: android networking retrofit retrofit2