我是使用Retrofit和Gson的新手,当我尝试使用POST方法向REST API发送请求时,我遇到如下错误。
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第4行第14行路径为$ .data
时为BEGIN_OBJECT
以下是我的java代码。
private void sendRequest(CheckEmail checkEmail){
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<CheckEmail> call = apiService.getEmailStatus(checkEmail);
call.enqueue(new Callback<CheckEmail>() {
@Override
public void onResponse(Call<CheckEmail> call, Response<CheckEmail> response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
CheckEmail checkEmail = gson.fromJson(String.valueOf(response.body().getData()), CheckEmail.class);
Toast.makeText(CheckEmailPage.this, "Successfull "+checkEmail, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<CheckEmail> call, Throwable t) {
Toast.makeText(CheckEmailPage.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
Log.e("Error Retrofit : ", String.valueOf(t));
}
});
}
以下是我的模特
public class CheckEmail {
private int code;
private String message;
private String[] data;
public CheckEmail(String message) {
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String[] getData() {
return data;
}
public void setData(String[] data) {
this.data = data;
}
}
这是我的类初始化URL和改造构建器
public class ApiClient {
public static final String BASE_URL = "https://f49d9d29-8471-4126-95b0-1ec3d18eda94.mock.pstmn.io/v1/";
private static Retrofit retrofit;
public static Retrofit getClient(){
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
if(retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
以下是ApiInterface
public interface ApiInterface {
@POST("users/check_status")
Call<CheckEmail> getEmailStatus(@Body CheckEmail email);
}
这是我从邮递员那里得到的json回复
{
"code": 1000,
"message": "OK",
"data": {
"id": "1",
"email": "test.test@gmail.com",
"status": "1",
"name": "testing",
"category": "2"
}
}
我的代码有什么问题? 提前谢谢你
答案 0 :(得分:0)
已经解析了响应(即响应&lt;&gt;响应)。无需使用gson builder
再次解析它。只需从响应中获取数据,如下所示:
String data[] = response.body().getData();
此处无需使用gson builder:
GsonBuilder gsonBuilder = new GsonBuilder(); // remove this code
Gson gson = gsonBuilder.create();// remove this code
CheckEmail checkEmail = gson.fromJson(String.valueOf(response.body().getData()), CheckEmail.class);// remove this code
答案 1 :(得分:0)
使用以下类替换数据。
public class Data {
@SerializedName("id")
@Expose
private String id;
@SerializedName("email")
@Expose
private String email;
@SerializedName("status")
@Expose
private String status;
@SerializedName("name")
@Expose
private String name;
@SerializedName("category")
@Expose
private String category;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
你的checkEmail类应该类似于以下内容(String []被Data类替换)
public class CheckEmail{
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private Data data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}