[英]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.