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 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
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
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. Thank you
Your data field is not an ArrayList as you specified in your RegisterUserResponse
class
You need to create a Data
class that will contains the fields id
username
password
...
and then in your RegisterUserResponse
class you will declare:
@SerializedName("data")
@Expose
private Data data;
instead of:
@SerializedName("data")
@Expose
private ArrayList data = new ArrayList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.