com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 需要一个字符串但是 BEGIN_OBJECT 在第 1 行第 39 列路径 $.message
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 39 path $.message
我正在使用改装在 android 中制作登录功能。我已经创建了一个用于登录验证的端点,然后我使用原始 (json) 使用 Postman 对其进行了测试并且它有效。但是当我使用改造将端点输入 android 时,我收到如下错误消息:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 需要一个字符串但是 BEGIN_OBJECT 在第 1 行第 39 列路径 $.message
谁能帮帮我?
这里是我的来源:
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
}
LoginActivity
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();
}
});
您确定:
@SerializedName("message")
private String message;
如果这个字段是对象,通常会出现这个错误。
你的JSON长得像
吗
"message":"test"
或类似的东西:
"message":{"field":"value"}
如果它是第二个变体,那么您应该简单地将字段更改为必要的类型。
我正在使用改装在 android 中制作登录功能。我已经创建了一个用于登录验证的端点,然后我使用原始 (json) 使用 Postman 对其进行了测试并且它有效。但是当我使用改造将端点输入 android 时,我收到如下错误消息:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 需要一个字符串但是 BEGIN_OBJECT 在第 1 行第 39 列路径 $.message
谁能帮帮我?
这里是我的来源:
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
}
LoginActivity
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();
}
});
您确定:
@SerializedName("message")
private String message;
如果这个字段是对象,通常会出现这个错误。 你的JSON长得像
吗"message":"test"
或类似的东西:
"message":{"field":"value"}
如果它是第二个变体,那么您应该简单地将字段更改为必要的类型。