【发布时间】:2019-02-27 09:25:41
【问题描述】:
数据库上的数据发布。它给出了 Result Json 格式。
{"response":"exist"}
当使用 Retrofit 库获取响应并使用 GSON 解析 json 时,它会继续 OnFailure 方法并给出错误。
call.enqueue(new Callback<Register>() {
@Override
public void onResponse(Call<Register> call, Response<Register> response) {
if (response.body().equals("ok")){
Toast.makeText(MainActivity.this, "Registration is Successfully", Toast.LENGTH_SHORT).show();
}else if (response.body().equals("exiest")){
Toast.makeText(MainActivity.this, "Already Exiest", Toast.LENGTH_SHORT).show();
}else if(response.body().equals("error")){
Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Register> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
错误是:
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
它是如何解决的,请指导我。
【问题讨论】:
-
您是否已将 addConverterFactory(GsonConverterFactory.create()) 添加到改造构建器中
-
在此处发布您的注册模型
-
Gson gson = new GsonBuilder() .setLenient() .create(); retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build();
-
@POST("Registration.php") 调用
registerUser(@Field("fname") String fName, @Field("mname") String mName, @Field("lname") String lName, @Field("mobile_no") String mobile, @Field("email_id") String email); -
@SerializedName("response") @Expose public String response; public String getResponse() { 返回响应; } public void setResponse(String response) { this.response = response; }