翻新预期为BEGIN_OBJECT,但为STRING

问题描述 投票:0回答:1

大家好,我是Sqareup改造图书馆的新手。我已经使用Laravel框架创建了一个Web服务。当我输入错误或格式错误的电话号码时,应该给我一个正确的错误,例如json。我正在尝试解析此JSON响应:

{
    "message": "The given data was invalid.",
    "errors": {
        "mobile_number": [
            "The mobile number format is invalid.",
            "The mobile number must be 11 digits."
        ]
    }
}

这是我的模特班:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;


public class LoginResult {
    @SerializedName("mobile_number")
    private String mobileNumber;
    private String action;
    @SerializedName("message")
    private String message;
    @SerializedName("errors")
    private Errors errors;

    public String getMobileNumber() {
        return mobileNumber;
    }

    public String getAction() {
        return action;
    }


    public String getMessage() {
        return message;
    }


    public Errors getErrors() {
        return errors;
    }


    public static class Errors {
        @SerializedName("mobile_number")
        @Expose
        private List<String> mobileNumber = null;

        public List<String> getMobileNumber() {
            return mobileNumber;
        }
    }
}

...

import com.google.gson.annotations.SerializedName;

public class Login {
    @SerializedName("mobile_number")
    private String mobileNumber;

    public String getMobileNumber() {
        return mobileNumber;
    }

    public Login(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }
}

我的界面:

import ir.husoft.kasbokar.models.Login;
import ir.husoft.kasbokar.models.LoginResult;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface ApiInterface {
    @POST("api/auth/login")
    Call<LoginResult> login(@Body Login login);
}

这里是ApiClient:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {
    public static final String BASE_URL = "http://127.0.0.1:8000/";
    public static Retrofit retrofit;

    public static Retrofit getApiClient() {
        if (retrofit == null) {
            Gson gson = new GsonBuilder().setLenient().create();
            retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson)).build();
        }

        return retrofit;
    }
}

我的成功方法:

ApiInterface api = ApiClient.getApiClient().create(ApiInterface.class);
String mobileNumber = etMobileNumber.getText().toString();
            Login login = new Login(mobileNumber);
            Call<LoginResult> verification = api.login(login);
            verification.enqueue(new Callback<LoginResult>() {
                @Override
                public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
                    if (response.isSuccessful()) {
                        Toast.makeText(LoginActivity.this, "Action is: " + response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(LoginActivity.this, "Error code: " + response.code(), Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<LoginResult> call, Throwable throwable) {
                    Toast.makeText(LoginActivity.this, throwable.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                }
            });

当我在成功方法上使用它会引发错误

预期为BEGIN_OBJECT,但在第1行第1列的STRING

这里怎么了?

android json gson retrofit
1个回答
0
投票

您是否尝试过从private String mobileNumber中删除LoginResult,并且mobileNumber是数组或字符串列表。

© www.soinside.com 2019 - 2024. All rights reserved.