简体   繁体   English

如何解决此错误 Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

[英]How do I solve this error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

I am working on a project with a django restframework backend,I have done the backend and tested it's working properly but now I want to connect my android application to the APIs but it is at this point that I want to fetch this resposnse我正在开发一个带有 django restframework 后端的项目,我已经完成了后端并测试了它是否正常工作,但现在我想将我的 android 应用程序连接到 API,但此时我想获取这个响应在此处输入图像描述 after a successfully registration and get some data to use in the next activity that I'm getting an error although data is saved in the database成功注册并获取一些数据以在下一个活动中使用后,尽管数据已保存在数据库中,但我遇到错误

Here is my APIClient Interface class这是我的 APIClient 接口 class

package com.example.covid_19_tracker.data.network;
import com.example.covid_19_tracker.data.network.model.AddLocationResponse;
import com.example.covid_19_tracker.data.network.model.LoginResponse;
import com.example.covid_19_tracker.data.network.model.RegisterDeviceResponse;
import com.example.covid_19_tracker.data.network.model.RegisterUserResponse;

import java.util.HashMap;

import io.reactivex.Observable;
import retrofit2.http.FieldMap;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface ApiInterface {

    @FormUrlEncoded
    @POST("api/locations/")
    Observable<AddLocationResponse> addLocation(@FieldMap HashMap<String, Object> params);

    @FormUrlEncoded
    @POST("api/devices/")
    Observable<RegisterDeviceResponse> registerDevice(@FieldMap HashMap<String, Object>   params);

    @FormUrlEncoded
    @POST("api/accounts/register/")
    Observable<RegisterUserResponse> registerUser(@FieldMap HashMap<String, Object> params);

    @FormUrlEncoded
    @POST("api/accounts/login/")
    Observable<LoginResponse> loginUser(@FieldMap HashMap<String, Object> params);
}

And here is my RegisterUserResponse.java model Class这是我的 RegisterUserResponse.java model Class

package com.example.covid_19_tracker.data.network.model;

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

import java.util.ArrayList;

public class RegisterUserResponse {
    @SerializedName("success")
    @Expose
    private String success;
    @SerializedName("status_code")
    @Expose
    private Integer status_code;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private ArrayList data = new ArrayList();
    @SerializedName("token")
    @Expose
    private String token;

    public RegisterUserResponse() {
    }

    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public Integer getStatus_code() {
        return status_code;
    }

    public void setStatus_code(Integer status_code) {
        this.status_code = status_code;
    }

    public String getMessage() {
        return message;
    }

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

    public ArrayList getData() {
        return data;
    }

    public void setData(ArrayList data) {
        this.data = data;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}

But I'm getting the error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ I'm a beginner in android.但我收到错误Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $我是 android 的初学者。 Thank you谢谢

Your data field is not an ArrayList as you specified in your RegisterUserResponse class您的数据字段不是您在RegisterUserResponse class 中指定的 ArrayList

You need to create a Data class that will contains the fields id username password ...您需要创建一个包含Data id username password ...

and then in your RegisterUserResponse class you will declare:然后在您的RegisterUserResponse class 中您将声明:

@SerializedName("data")
    @Expose
    private Data data;

instead of:代替:

@SerializedName("data")
    @Expose
    private ArrayList data = new ArrayList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 预期为BEGIN_ARRAY,但位于第1行第2列路径的BEGIN_OBJECT $ - Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path$ 应该是BEGIN_ARRAY,但在第1行第2列路径$处是BEGIN_OBJECT吗? - Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $? Gson 错误:“预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径为 BEGIN_OBJECT” - Gson error: “Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path” 转换json与gson错误预期BEGIN_OBJECT但是BEGIN_ARRAY在第1行第2列路径$ - converting json with gson error Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ Android Studio ArrayList 错误:预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径为 BEGIN_OBJECT - Android Studio ArrayList Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ 我无法获得 JSON 数据“预期为 BEGIN_ARRAY,但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT” - I can't get the JSON data "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $" 预期为BEGIN_OBJECT,但在第1行570列为BEGIN_ARRAY - Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 570 RETROFIT 2预期为BEGIN_ARRAY,但在第1行第2列为BEGIN_OBJECT - RETROFIT 2 Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 预期为BEGIN_OBJECT,但在第1行第13列为BEGIN_ARRAY - Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 13 预期BEGIN_ARRAY但在第1行第2列是BEGIN_OBJECT - Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM