在 Retrofit 中使用 Get 方法获取 gson 时出错
Error in getting gson while using Get Method in Retrofit
我不明白我在 String 中设置的所有内容。但仍然有这个问题。当我测试我的网络时 API 在这件事上没有问题..但是当我实施时遇到了这个问题..
这是我的错误
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path
这是我调用 get Section
的代码
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemID) {
if (position >= 0 && position < goodModelArrayList.size()) {
String section= goodModelArrayList.get(position).getSection();
//getSelectedCategoryData(section);
restService.getService().getSection(section, new Callback<VW_AC_Line_>() {
@Override
public void success(VW_AC_Line_ student, Response response) {
Toast.makeText(Viewattendance.this, "success", Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(Viewattendance.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(Viewattendance.this, "Selected Category Does not Exist!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
这里是我的数据class要调用
public class VW_AC_Line_ {
public int ID;
public String Section;
public int getID(){
return ID;
}
public void setID(int id){
this.ID = id;
}
public String getSection() {
return Section;
}
public void setSection(String section) {
this.Section = section;
}
}
这是我的网页调用方式api
@GET("/api/VW_AC_LINE_/deparment/{section}")
public void getSection(@Path("section") String section, Callback<VW_AC_Line_> callback);
这是网络 api 呼叫
[{"ID":2665,"Section":"IT"}].
我认为您低于响应(对象数组)但期待对象。
对象数组:[{VW_AC_Line_:{
"a":2,"b":3}}]
预期:{VW_AC_Line_:{
"a":2,"b":3}}
restService.getService().getSection(section, new Callback<ArrayList<VW_AC_Line_>>() {
@Override
public void success(ArrayList<VW_AC_Line_> student, Response response) {
Toast.makeText(Viewattendance.this, "success", Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(Viewattendance.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
称其为数组列表,因为网络API在数组中调用
我不明白我在 String 中设置的所有内容。但仍然有这个问题。当我测试我的网络时 API 在这件事上没有问题..但是当我实施时遇到了这个问题..
这是我的错误
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path
这是我调用 get Section
的代码 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long itemID) {
if (position >= 0 && position < goodModelArrayList.size()) {
String section= goodModelArrayList.get(position).getSection();
//getSelectedCategoryData(section);
restService.getService().getSection(section, new Callback<VW_AC_Line_>() {
@Override
public void success(VW_AC_Line_ student, Response response) {
Toast.makeText(Viewattendance.this, "success", Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(Viewattendance.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
} else {
Toast.makeText(Viewattendance.this, "Selected Category Does not Exist!", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
这里是我的数据class要调用
public class VW_AC_Line_ {
public int ID;
public String Section;
public int getID(){
return ID;
}
public void setID(int id){
this.ID = id;
}
public String getSection() {
return Section;
}
public void setSection(String section) {
this.Section = section;
}
}
这是我的网页调用方式api
@GET("/api/VW_AC_LINE_/deparment/{section}")
public void getSection(@Path("section") String section, Callback<VW_AC_Line_> callback);
这是网络 api 呼叫
[{"ID":2665,"Section":"IT"}].
我认为您低于响应(对象数组)但期待对象。
对象数组:[{VW_AC_Line_:{ "a":2,"b":3}}]
预期:{VW_AC_Line_:{ "a":2,"b":3}}
restService.getService().getSection(section, new Callback<ArrayList<VW_AC_Line_>>() {
@Override
public void success(ArrayList<VW_AC_Line_> student, Response response) {
Toast.makeText(Viewattendance.this, "success", Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(Viewattendance.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
称其为数组列表,因为网络API在数组中调用