我正在尝试使用Retrofit获取和发布数据但它引发异常 Fsaailurecom.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但在第1行第1行STRING 我尝试了StackOverflow社区建议的所有可能的解决方案,但仍然存在问题退出:( 任何帮助.. !!
PS:在brwoser和Localhost中,我能够获得API call http://onesignaldemo.rf.gd/test/get_user.php的正确Json响应。邮递员返回此网站需要使用Javascript才能运行,请在浏览器中启用Javascript或使用支持Javascript的浏览器
API CALL
private void getUsersFromDB() {
userservice= ApiUtils.getUserservice();
userservice.getUsers().enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
Log.d("RESULT", "onResponse: --------------------------------SUCCESS");
List<Result> userresponses = response.body().getResult();
Log.d("ANS", "Number of movies received: " + userresponses.size());
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
Log.d("RESULT", "onResponse: --------------------------------Failure" +t);
}
});
}
UserModal.java
public class UserModel {
@SerializedName("id")
@Expose
private String id;
@SerializedName("user_name")
@Expose
private String userName;
@SerializedName("user_password")
@Expose
private String userPassword;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
UserResponse.java
public class UserResponse {
@SerializedName("result")
@Expose
private List<Result> result = null;
@SerializedName("error_code")
@Expose
private Integer errorCode;
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
public Integer getErrorCode() {
return errorCode;
}
public void setErrorCode(Integer errorCode) {
this.errorCode = errorCode;
}
}
接口
public interface UserService {
@GET("/get_user.php")
Call<UserResponse> getUsers();
@FormUrlEncoded
@POST("/insert_user.php")
Call<DBResponse> insertUsers(@Field("user_name") String username,
@Field("user_password") String userpassword);
}
这是我的Json
{"result":[{"Users":{"id":"1","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"2","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"3","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"4","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"5","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"6","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"7","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"8","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"9","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"10","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"11","user_name":"raj","user_password":"rajesh123"}},{"Users":{"id":"12","user_name":"raj","user_password":"rajesh123"}}]}
答案 0 :(得分:1)
服务器只返回一个String,而不是一个对象。试着看看Fiddler的答案是什么,或者只是在Postman中创建一个具有相同请求数据的请求来查看答案。
正如我所看到的,在result
中,您有一个对象列表,User
中编码的对象会返回您的UserModal.java
。因此,您需要在result
和UserModal.java
之间创建一个中间对象。类似的东西:
public IntermediarUser {
@SerializedName("User")
public UserModal user;
// getter, setter, etc
}
如果您不想这样做,请检查您的服务器实施是否有云。
答案 1 :(得分:0)
可能是错的但看起来你的JSON返回一个结果对象,它有一个用户列表而不是结果列表?所以你应该有一个带有List属性的Result pojo,那么你认为你的用户对象应该有三个字符串:id,user_name和user_password。该错误几乎总是(如果不是总是)指的是pojos / data模型结构与JSON结构的不匹配。