[英]Gson: Expected a string but was BEGIN_OBJECT at line 1 column 3 path $[0]
[英]Gson error expected begin_object but was string at line 1 column 1 path $
希望您能幫我解決我無法解決的Gson問題。 很顯然,這很安靜,因為我發現了許多與此主題相關的主題,但是沒有設法使用答案。
我有這個錯誤:
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期為BEGIN_OBjECT,但位於第1行第1列的路徑$
這是我的傑森:
{"valeurs":[{"Ident":"1","Lien":"r8WzdMerigo","Categorie":"1"},{"Ident":"2","Lien":"neqgJGz08Fw","Categorie":"2"}],"success":1}
然后我的POJO:
public class gitmodel {
@SerializedName("Ident")
@Expose
private int Ident;
@SerializedName("Lien")
@Expose
private String Lien;
@SerializedName("Categorie")
@Expose
private int Categorie;
public int getIdent() {return Ident;}
public String getLien() {
return Lien;
}
public int getCategorie() {
return Categorie;
}
}
最后在Main活動中:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(API)
.build();
gitapi git = restAdapter.create(gitapi.class);
git.getFeed("affichage_bd.php", new Callback<gitmodel>() {
@Override
public void success(gitmodel gitmodel, Response response) {
tv.setText("Numero categorie :" + gitmodel.getCategorie() + "\nLien :" + gitmodel.getLien() + "\nIdentification :" + gitmodel.getIdent());
}
@Override
public void failure(RetrofitError error) {
tv.setText(error.getMessage());
}
});
}
});
你能告訴我發生了什么嗎?
您缺少一個級別。 您在Callback <>中設置的POJO應該為:
public class MyModel
{
List<gitmodel> valeurs;
public List<gitmodel> getValeurs()
{
return valeurs;
}
}
編輯:
您應該將MyModel類保存在項目中的某個位置,然后將接口更改為:
public interface gitapi {
@GET("/users/{user}")
public void getFeed(@Path("user") String user, Callback<MyModel> myModel);
}
因此,在您的MainActivity中 :
git.getFeed("affichage_bd.php", new Callback<MyModel>() {
@Override
public void success(MyModel myModel, Response response) {
List<gitmodel> valeurs = myModel.getValeurs();
// here you can iterate through the elements on the list
}
@Override
public void failure(RetrofitError error) {
tv.setText(error.getMessage());
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.