我如何从我的 api 调用中获取此对象我正在使用改造
How can i get this Object from my api call am using retrofit
我正在尝试访问我的 api 那个 return 一个包含数据数组的对象,但我无法访问 我收到一条错误消息,提示 BEGIN_ARRAY 但 BEGIN_OBJECT在第 1 行第 2 列路径 $ 正在对我的 android 应用程序进行改造
这是 api return 的意思
{
"students": [
{
"_id": "5e74775518b0f00c0123925b",
"registrationno": "IWR/D/2016/0024",
"firstname": "Mariam",
"lastname": "Wamigomba",
"amount": 10000,
"reason": "lost key ",
"__v": 0
}]}
这是我的界面
public interface OcappJsonApiSNAL {
@GET("getStudents")
Call<List<StudentClearanceSNAL>> getStudents(@Header ("Authorization") String token);
}
我的javaclass
OcappJsonApiSNAL ocappJsonApi = retrofit.create (OcappJsonApiSNAL.class);
Call<List<StudentClearanceSNAL>> listCall = ocappJsonApi.getStudents ("Bearer token");
listCall.enqueue (new Callback<List<StudentClearanceSNAL>> () {
@Override
public void onResponse(Call<List<StudentClearanceSNAL>> call, Response<List<StudentClearanceSNAL>> response) {
if(!response.isSuccessful ()) {
Toast.makeText (getActivity (),"From OCApp " + response.code (),Toast.LENGTH_LONG).show ();
return;
}
List<StudentClearanceSNAL> studentClearancess = response.body ();
for ( final StudentClearanceSNAL studentClearance: studentClearancess)
{
获取错误
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Call<List<StudentClearanceSNAL>> listCall = ocappJsonApi.getStudents ("Bearer token");
您只需使用列表创建新模型,而不是外部地图列表
创建新模型:
Class StudentClearanceSNALList{
@SerializedName("students") // it’s must match with your Jason object name
List<StudentClearanceSNAL> studentClearanceSNAL = new Arraylist<>();
//getter and setter
}
然后更新您的界面并class喜欢下面的代码:
接口:
public interface OcappJsonApiSNAL {
@GET("getStudents")
Call<StudentClearanceSNALList> getStudents(@Header ("Authorization") String token);
}
Class :
OcappJsonApiSNAL ocappJsonApi = retrofit.create (OcappJsonApiSNAL.class);
Call<StudentClearanceSNALList> listCall = ocappJsonApi.getStudents ("Bearer token");
listCall.enqueue (new Callback<StudentClearanceSNALList> () {
@Override
public void onResponse(Call<StudentClearanceSNALList> call, Response<StudentClearanceSNALList> response) {
if(!response.isSuccessful ()) {
Toast.makeText (getActivity (),"From OCApp " + response.code (),Toast.LENGTH_LONG).show ();
return;
}
StudentClearanceSNALList studentClearancess = response.body ();
为什么我建议这意味着它是期望列表但是你传递了对象所以它不接受所以请使用上面的代码。
希望对你有帮助谢谢
我正在尝试访问我的 api 那个 return 一个包含数据数组的对象,但我无法访问 我收到一条错误消息,提示 BEGIN_ARRAY 但 BEGIN_OBJECT在第 1 行第 2 列路径 $ 正在对我的 android 应用程序进行改造
这是 api return 的意思
{
"students": [
{
"_id": "5e74775518b0f00c0123925b",
"registrationno": "IWR/D/2016/0024",
"firstname": "Mariam",
"lastname": "Wamigomba",
"amount": 10000,
"reason": "lost key ",
"__v": 0
}]}
这是我的界面
public interface OcappJsonApiSNAL {
@GET("getStudents")
Call<List<StudentClearanceSNAL>> getStudents(@Header ("Authorization") String token);
}
我的javaclass
OcappJsonApiSNAL ocappJsonApi = retrofit.create (OcappJsonApiSNAL.class);
Call<List<StudentClearanceSNAL>> listCall = ocappJsonApi.getStudents ("Bearer token");
listCall.enqueue (new Callback<List<StudentClearanceSNAL>> () {
@Override
public void onResponse(Call<List<StudentClearanceSNAL>> call, Response<List<StudentClearanceSNAL>> response) {
if(!response.isSuccessful ()) {
Toast.makeText (getActivity (),"From OCApp " + response.code (),Toast.LENGTH_LONG).show ();
return;
}
List<StudentClearanceSNAL> studentClearancess = response.body ();
for ( final StudentClearanceSNAL studentClearance: studentClearancess)
{
获取错误
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
Call<List<StudentClearanceSNAL>> listCall = ocappJsonApi.getStudents ("Bearer token");
您只需使用列表创建新模型,而不是外部地图列表
创建新模型:
Class StudentClearanceSNALList{
@SerializedName("students") // it’s must match with your Jason object name
List<StudentClearanceSNAL> studentClearanceSNAL = new Arraylist<>();
//getter and setter
}
然后更新您的界面并class喜欢下面的代码:
接口:
public interface OcappJsonApiSNAL {
@GET("getStudents")
Call<StudentClearanceSNALList> getStudents(@Header ("Authorization") String token);
}
Class :
OcappJsonApiSNAL ocappJsonApi = retrofit.create (OcappJsonApiSNAL.class);
Call<StudentClearanceSNALList> listCall = ocappJsonApi.getStudents ("Bearer token");
listCall.enqueue (new Callback<StudentClearanceSNALList> () {
@Override
public void onResponse(Call<StudentClearanceSNALList> call, Response<StudentClearanceSNALList> response) {
if(!response.isSuccessful ()) {
Toast.makeText (getActivity (),"From OCApp " + response.code (),Toast.LENGTH_LONG).show ();
return;
}
StudentClearanceSNALList studentClearancess = response.body ();
为什么我建议这意味着它是期望列表但是你传递了对象所以它不接受所以请使用上面的代码。
希望对你有帮助谢谢