我正在使用改造在 android 中創建登錄功能。我創建了一個用于登錄驗證的端點,然后我使用 Postman 使用 raw (json) 對其進行了測驗,并且它作業正常。但是,當我使用改造將端點輸入 android 時,我收到如下錯誤訊息:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:需要一個字串,但在第 1 行第 39 列路徑 $.message 處為 BEGIN_OBJECT
誰能幫我?
所以這里是我的來源:
客戶端
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;
}
}
認證介面
public interface AuthInterface {
@Headers("Content-Type: application/json")
@POST("auth/login")
Call<AuthPost> authPostCall(@Body String body);
}
授權郵局
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();
}
});
uj5u.com熱心網友回復:
你確定:
@SerializedName("message")
private String message;
如果此欄位是物件,通常會出現此錯誤。你的 JSON 看起來像嗎
"message":"test"
或類似的東西:
"message":{"field":"value"}
如果它是第二個變體,那么您應該簡單地將欄位更改為必要的型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/403188.html
標籤:
