【问题标题】:Retrofit gson exception改造 gson 异常
【发布时间】: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


【解决方案1】:

例外: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为 BEGIN_OBJECT 但在第 1 行第 2 列路径为 BEGIN_ARRAY

在反序列化时发生,Gson 期待一个 JSON 对象,但找到了一个 JSON 数组。由于它无法从一个转换到另一个,它抛出了这个异常。 请参考:GSON throwing "Expected BEGIN_OBJECT but was BEGIN_ARRAY"?

试试这个:

 @FormUrlEncoded
    @POST("PastMain")
    Call<List<PastMemo>> getPastMain(
            @FieldMap Map<String, String> user
    );

改造代码:

Call<List<PastMemo>> call = webService.getPastMain(map);
call.enqueue(new Callback<List<PastMemo>>() {
    @Override
    public void onResponse(Call<List<PastMemo>> call, Response<List<PastMemo>> response) {

     List<PastMemo> ls = response.body();
     //your implementation here
    }

    @Override
    public void onFailure(Call<PastMemo> call, Throwable t) {
        Toast.makeText(getActivity(), "SignIn Denied", Toast.LENGTH_SHORT).show();
    }
});

【讨论】:

  • 那我应该怎么做才能解决这个问题呢?我一直在寻找解决方案。还是谢谢你。
  • 这不是答案。必须是评论
  • 谢谢。也许更多的谷歌搜索会帮助我解决这个问题
  • 而不是捕获对象,而是为 List 之类的列表做准备。或者您可以先使用 Postman 检查响应并据此确定结构。
  • 你的意思是json是JSONArray所以我需要使用list来获取数据??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2016-06-18
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 2020-06-20
  • 1970-01-01
相关资源
最近更新 更多