首页 > 解决方案 > java.lang.illegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 1 列路径为字符串 $ Rest api Gives OnFailed

问题描述

我的 json 回复

{
    "error": 200,
    "message": "New Admin",
    "adminUser": [
        {
            "adminUserId": "9",
            "adminUserGmail": "sagevadiyagulabadada123",
            "companyName": "",
            "name": "",
            "signUpDate": "2020-12-12",
            "expiryDate": "2020-12-12",
            "mobileNumber": "70465524937",
            "phoneNumber": "7065424937",
            "subscriptionPlan": "free",
            "profilePicPath": "image path",
            "gstNumber": "24AADSA5640A1Z3",
            "companyBillingAddress": "Guajrat India",
            "companyShippingAddress": "Gujarat India"
        }
    ]
}

我的电话

    @Headers("Content-Type: application/json")
    @FormUrlEncoded
    @POST("insertAdmin.php")
    Call<GetLogInResponse> loginAdmin(
            @Field("adminUserGmail") String adminGmail,
            @Field("companyName") String companyName,
            @Field("name") String name,
            @Field("signUpDate") String signUpDate,
            @Field("expiryDate") String expiryDate,
            @Field("mobileNumber") String mobileNumber,
            @Field("phoneNumber") String phoneNumber,
            @Field("subscriptionPlan") String subPlan,
            @Field("profilePicPath") String profilePicPath,
            @Field("gstNumber") String gstNumber,
            @Field("companyBillingAddress") String billingAddress,
            @Field("companyShippingAddress") String shippingAddress
    );

我这样打电话

            Call<GetLogInResponse> call = RetrofitMain
                    .getRetrofitMain()
                    .getApis()
                    .loginAdmin(mail,
                            " ",
                            firebaseUser.getDisplayName(),
                            todayDate,
                            expiryDate,
                            " ",
                            " ",
                            "Free Trails",
                            " ",
                            " ",
                            " ",
                            " ");

            call.enqueue(new Callback<GetLogInResponse>() {
                @Override
                public void onResponse(Call<GetLogInResponse> call, Response<GetLogInResponse> response) {

                    GetLogInResponse logInResponse = response.body();
                    if (response.isSuccessful()){
                        switch (logInResponse.getMessage()) {

                            case "Already Exist": {

                                Toast.makeText(AdminLogin.this, "Already Exist.", Toast.LENGTH_LONG).show();
                                AdminUser adminUser = logInResponse.getAdminUser().get(0);
                                setData(adminUser);
                                sendUserToHome();
                                break;
                            }
                            case "New Admin": {
                                Toast.makeText(AdminLogin.this, "New Admin", Toast.LENGTH_LONG).show();
                                AdminUser adminUser = logInResponse.getAdminUser().get(0);
                                setNewData(adminUser);
                                sendUserToHome();

                                break;
                            }
                            case "Login Failed Try Again":
                                Toast.makeText(AdminLogin.this, "Login Failed!!! Try Again.", Toast.LENGTH_LONG).show();
                                break;
                        }
                    }else {
                        Toast.makeText(AdminLogin.this, "Login Failed!!! Try Again.", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(Call<GetLogInResponse> call, Throwable t) {
                    Toast.makeText(AdminLogin.this, "Error" + t.getLocalizedMessage() + "  ++ "+ t.getMessage(), Toast.LENGTH_LONG).show();
                }
            });


        }else {
            Toast.makeText(AdminLogin.this, "Authentication Failed", Toast.LENGTH_LONG).show();
            startActivity(new Intent(AdminLogin.this, LogInActivity.class));
            finish();
        }

我的模型对象 AdminUser

public class AdminUser {

    @SerializedName("adminUserId")
    @Expose
    private String adminUserId;
    @SerializedName("adminUserGmail")
    @Expose
    private String adminUserGmail;
    @SerializedName("companyName")
    @Expose
    private String companyName;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("signUpDate")
    @Expose
    private String signUpDate;
    @SerializedName("expiryDate")
    @Expose
    private String expiryDate;
    @SerializedName("mobileNumber")
    @Expose
    private String mobileNumber;
    @SerializedName("phoneNumber")
    @Expose
    private String phoneNumber;
    @SerializedName("subscriptionPlan")
    @Expose
    private String subscriptionPlan;
    @SerializedName("profilePicPath")
    @Expose
    private String profilePicPath;
    @SerializedName("gstNumber")
    @Expose
    private String gstNumber;
    @SerializedName("companyBillingAddress")
    @Expose
    private String companyBillingAddress;
    @SerializedName("companyShippingAddress")
    @Expose
    private String companyShippingAddress;

    public String getAdminUserId() {
        return adminUserId;
    }

    public void setAdminUserId(String adminUserId) {
        this.adminUserId = adminUserId;
    }

    public String getAdminUserGmail() {
        return adminUserGmail;
    }

    public void setAdminUserGmail(String adminUserGmail) {
        this.adminUserGmail = adminUserGmail;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSignUpDate() {
        return signUpDate;
    }

    public void setSignUpDate(String signUpDate) {
        this.signUpDate = signUpDate;
    }

    public String getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getSubscriptionPlan() {
        return subscriptionPlan;
    }

    public void setSubscriptionPlan(String subscriptionPlan) {
        this.subscriptionPlan = subscriptionPlan;
    }

    public String getProfilePicPath() {
        return profilePicPath;
    }

    public void setProfilePicPath(String profilePicPath) {
        this.profilePicPath = profilePicPath;
    }

    public String getGstNumber() {
        return gstNumber;
    }

    public void setGstNumber(String gstNumber) {
        this.gstNumber = gstNumber;
    }

    public String getCompanyBillingAddress() {
        return companyBillingAddress;
    }

    public void setCompanyBillingAddress(String companyBillingAddress) {
        this.companyBillingAddress = companyBillingAddress;
    }

    public String getCompanyShippingAddress() {
        return companyShippingAddress;
    }

    public void setCompanyShippingAddress(String companyShippingAddress) {
        this.companyShippingAddress = companyShippingAddress;
    }

}

我的模型对象响应

public class GetLogInResponse {

    @SerializedName("error")
    @Expose
    private int error;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("adminUser")
    @Expose
    private List<AdminUser> adminUser = null;

    public int getError() {
        return error;
    }

    public void setError(int error) {
        this.error = error;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<AdminUser> getAdminUser() {
        return adminUser;
    }

    public void setAdminUser(List<AdminUser> adminUser) {
        this.adminUser = adminUser;
    }
}

我的错误显示在吐司中,像这样 java.lang.illegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

java.lang.illegalStateException:应为 BEGIN_OBJECT,但在第 1 行第 1 列路径 $

我该怎么办?谢谢你的帮助。

日志

    <b>Notice</b>:  Undefined index: adminUserGmail in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>9</b><br />
    <br />
    <b>Warning</b>:  mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>14</b><br />
I/okhttp.OkHttpClient: <br />
    <b>Notice</b>:  Undefined index: companyName in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>30</b><br />
    <br />
    <b>Notice</b>:  Undefined index: name in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>31</b><br />
    <br />
    <b>Notice</b>:  Undefined index: signUpDate in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>32</b><br />
    <br />
    <b>Notice</b>:  Undefined index: expiryDate in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>33</b><br />
    <br />
    <b>Notice</b>:  Undefined index: mobileNumber in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>34</b><br />
    <br />
    <b>Notice</b>:  Undefined index: phoneNumber in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>35</b><br />
    <br />
    <b>Notice</b>:  Undefined index: subscriptionPlan in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>36</b><br />
    <br />
    <b>Notice</b>:  Undefined index: profilePicPath in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>37</b><br />
    <br />
    <b>Notice</b>:  Undefined index: gstNumber in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>38</b><br />
    <br />
    <b>Notice</b>:  Undefined index: companyBillingAddress in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>39</b><br />
    <br />
    <b>Notice</b>:  Undefined index: companyShippingAddress in <b>/storage/ssd4/543/15633543/public_html/insertAdmin.php</b> on line <b>40</b><br />
    {"error":200,"message":"Login Failed Try Again","adminUser":[]}

标签: androidrest

解决方案


尝试使用改造拦截器。它将在日志中向您显示请求和响应,您可以查看您的响应有哪些错误。

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .build();

Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Common.URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

推荐阅读