gpt4 book ai didi

android - 预期 BEGIN_OBJECT 但在第 1 行第 1 列路径中为 STRING - Laravel 到 Retrofit 2

转载 作者:太空狗 更新时间:2023-10-29 14:36:15 26 4
gpt4 key购买 nike

我使用 Laravel 构建了一个简单的 API,它运行良好。我的 Android 应用程序应该使用来自此 API 的数据,但对于某些 JSON 响应,我收到错误

Expected BEGIN_OBJECT but was STRING at line 1 column 1 path

在收到此错误之前,错误是以下错误:

Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path

然后我setLenient(true)来解决。

我在许多其他帖子中看到问题是格式错误的 JSON,但它似乎一切正常。

给我错误的 JSON 响应:

{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImRhMWUxNzU4YmI1YTFjMjAzZDI1MDk5OTM4YTljMjgwNDUyMjYzMDFhNjZiZmViZmJlMTQyY2FmNGU4OGUwMzc2NTZkN2RmZjI4NDUxMTEzIn0.eyJhdWQiOiIxIiwianRpIjoiZGExZTE3NThiYjVhMWMyMDNkMjUwOTk5MzhhOWMyODA0NTIyNjMwMWE2NmJmZWJmYmUxNDJjYWY0ZTg4ZTAzNzY1NmQ3ZGZmMjg0NTExMTMiLCJpYXQiOjE1NTE3OTIzMzAsIm5iZiI6MTU1MTc5MjMzMCwiZXhwIjoxNTgzNDE0NzMwLCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.bi4l9mpUDLoYMLWwd1JyCAfoN2GtwpnpEmGnzsoDoIP_rcTWfT9IsixX4iwtbjSdOTBFkqSD9oGz8KncI24spgnKhofZelClSP6tNtI6P8q2b_PmnefytmNfu_fWBAvSTD2Tia8-cMCu_eF9QfNoOv_9JCYAaY__DY2W_zI3u8uLv25EZQPgBiW7MIT_gm9ZnD6JJlCL4rkgUuhdDG8lkjHCryAYVBqXsGLizl6YdgBp8zMYzONzjxT4nhTARL_aywE9ejZCt5Mkh5qvoGms4zkMMAW0OIYVP_puB7MSPJAriCR1kaBO06MSNGcfAdorn5VDpfwFV8t25mftOeaM1x8IvWT1K0_Hi0BtdV2twtJ-_uHcmIn8QiF3Tc6On7YWWWGEfJw0vAGqK5RaYtWVSo5iWbAiD5rsmCQRQDsg4GnEH9AnYHjcjortBe01YuimQ4TMSIgWLpRfExwcPgQkonklWqODcE8nvuWzyKUirmm56gGj1wBZciqrYsDRTImP4Ntv1Q5Tb3r2PoFlqxfn3_qux1OQpTyzHGEOEaE9ZQnNoELjwGd5nUuFOYCOdwafIUsG0atHtoPBj_S6jTsJJdIWqQzCexchpkwBUgP5i6o1QCai7BcOnTXKJ3V6kV17SGtiP7gqTM9fvESD0BIuK4Xd0dsA8mc67_HwLJyhpXM",
"token_type": "Bearer",
"expires_at": "2019-03-19"
}

工作 JSON 响应:

{
"message": "The given data was invalid.",
"errors": {
"username": [
"The username has already been taken."
]
}
}

我用 JSONLint 测试了 JSON它说 JSON 一切都很好

不工作 - Laravel JSON 响应

return response()->json([
'access_token' => $tokenResult->accessToken,
'token_type' => 'Bearer',
'expires_at' => Carbon::parse(
$tokenResult->token->expires_at
)->toDateString()
], 200);

工作 - Laravel JSON 响应

    return response()->json([
'message'=>'Cadastro realizado.',
'user_id' => $user->id
], 201);

Android - 登录

btnSignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: " + etSigninUser.getText().toString());
Log.d(TAG, "onClick: " + etSigninPassword.getText().toString());
Call<LoginResponse> call = apiService.userLogin(
etSigninUser.getText().toString(),
etSigninPassword.getText().toString(),
false
);

call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
Log.d(TAG, "onResponse: " + response.code());
}

@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.toString() + " " + t.getCause());
Log.d(TAG, "onFailure: " + t.toString());
}
});
}
});

Android - 登录响应模型

    public class LoginResponse {

@SerializedName("access_token")
@Expose
private String accessToken;
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("expires_at")
@Expose
private String expiresAt;

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public String getTokenType() {
return tokenType;
}

public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}

public String getExpiresAt() {
return expiresAt;
}

public void setExpiresAt(String expiresAt) {
this.expiresAt = expiresAt;
}
}

**Android - Retrofit 2 调用 **

@FormUrlEncoded
@POST("auth/login")
Call<LoginResponse> userLogin(
@Field("username") String username,
@Field("password") String password,
@Field("remember_me") boolean remember_me
);

我看不出我的代码有什么问题,一切看起来都很好,为什么我会收到这个错误?JSON 是否格式错误?

编辑 1 - postman

Postman

最佳答案

我遇到了这个问题,我花了很多时间才弄明白。

我在这里找到了解决方案:https://stackoverflow.com/a/56903479/3971619

TLDR:用于创建 Retrofit 对象的 BASE_URL 变量在/之后不应有任何内容

替换:

val authApiService = Retrofit.Builder()
.baseUrl("https://test.com/api/test") //There is path after the /
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build().create(AuthService::class.java)

通过

val authApiService = Retrofit.Builder()
.baseUrl("https://test.com/") //It should only be the base url without a path
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build().create(ServiceClass::class.java)

关于android - 预期 BEGIN_OBJECT 但在第 1 行第 1 列路径中为 STRING - Laravel 到 Retrofit 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55004165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com