我已经检查了另一个相关的问题,但是他们的建议都不适合我的情况。我正在使用retreate 2.2和GSON来获得Rest响应:这是响应的样子:
{"idUser":0,"nom":"kaddour","prenom":"hanedi","login":"hanedi@gmail.com","password":null,"genre":"f","adresse":"ettahrir","codePostal":8050,"tel":20333473,"active":0,"panneaux":null,"lesalertes":null,"paysuser":null,"ville":null}
它是一个有效的json格式,但是每当我使用此代码调用时,我都会得到预期的Begin_Object异常:
apiService = RestService.createService(SolarAPIService.class);
Call<User> call = apiService.authentif(email, pass);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, retrofit2.Response<User> response) {
user=response.body();}
我找不到哪里出了问题。任何帮助表示赞赏!编辑这是我的用户类:
public class User {
@SerializedName("idUser")
@Expose
private int idUser;
@SerializedName("nom")
@Expose
private String nom;
@SerializedName("prenom")
@Expose
private String prenom;
@SerializedName("login")
@Expose
private String login;
@SerializedName("password")
@Expose
private String password;
@SerializedName("genre")
@Expose
private String genre;
@SerializedName("adresse")
@Expose
private String adresse;
@SerializedName("codePostal")
@Expose
private int codePostal;
@SerializedName("tel")
@Expose
private int tel;
@SerializedName("active")
@Expose
private int active;
@SerializedName("panneaux")
@Expose
private List<PanneauV> panneaux;
@SerializedName("lesalertes")
@Expose
private List<Alerte> lesalertes;
@SerializedName("paysuser")
@Expose
private Pays paysuser;
@SerializedName("ville")
@Expose
private Ville ville;
//Constructor with arguments and Constructor without arguments
//getters and setters
您的User类必须实现Serializable。
如果名称相同,则无需使用 SerializeName 覆盖属性。我认为您也可以删除@expose。