【问题标题】:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.messagecom.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为字符串,但在第 1 行第 39 列路径 $.message 为 BEGIN_OBJECT
【发布时间】:2022-01-04 08:35:52
【问题描述】:

我正在使用改造在 android 中制作登录功能。我已经为登录验证创建了一个端点,然后我使用 Postman 使用 raw (json) 对其进行了测试,并且它工作正常。但是当我使用改造将端点输入到 android 中时,我收到如下错误消息:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 应为字符串,但在第 1 行第 39 列路径 $.message 处为 BEGIN_OBJECT

谁能帮帮我?

这里是我的来源:

ApiClient

public class ApiClient {

    public static final String BASE_URL = "";
    public static Retrofit retrofit;

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

        return retrofit;
    }

}

AuthInterface

public interface AuthInterface {

    @Headers("Content-Type: application/json")
    @POST("auth/login")
    Call<AuthPost> authPostCall(@Body String body);
}

AuthPost

public class AuthPost {

    @SerializedName("status")
    private String status;
    @SerializedName("error_code")
    private int error_code;
    @SerializedName("message")
    private String message;
    @SerializedName("token")
    private String token;

    ...getter and setter
}

登录活动

                JSONObject payload = new JSONObject();
                try {
                    payload.put("login_username", loginUsernameText);
                    payload.put("login_password", loginPasswordText);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Call<AuthPost> authPostCall = authInterface.authPostCall(payload.toString());
                authPostCall.enqueue(new Callback<AuthPost>() {
                    @Override
                    public void onResponse(Call<AuthPost> call, Response<AuthPost> response) {
                        if (response.code() == 200) {
                            
                        } else {
                            
                        }
                    }

                    @Override
                    public void onFailure(Call<AuthPost> call, Throwable t) {
                        t.printStackTrace();
                    }
                });

【问题讨论】:

  • 错误是不言自明的......你不明白什么?

标签: java json gson


【解决方案1】:

你确定:

@SerializedName("message")
    private String message;

如果此字段为对象,通常会出现此错误。 你的 JSON 看起来像吗

"message":"test"

或类似的东西:

"message":{"field":"value"}

如果它是第二个变体,那么您应该简单地将字段更改为必要的类型。

【讨论】:

  • 非常感谢,我刚刚发现了错误,结果和你描述的一样。我在 Android 上发送 json 数据时遇到问题
猜你喜欢
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 2022-07-20
  • 1970-01-01
  • 2019-11-27
  • 2020-04-10
  • 2021-09-03
  • 2020-01-29
相关资源
最近更新 更多