英文:
How can i get this Object from my api call am using retrofit
问题
以下是翻译好的内容:
尝试访问返回带有数据数组的对象的 API,但是我无法访问,出现错误,错误信息为“在第1行第2列处预期为 BEGIN_ARRAY,但实际为 BEGIN_OBJECT”。我在我的 Android 应用程序中使用 Retrofit。
调用该 API 时返回的内容如下:
{
"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);
}
在我的 Java 类中:
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(), "来自 OCApp " + response.code(), Toast.LENGTH_LONG).show();
return;
}
List<StudentClearanceSNAL> studentClearancess = response.body();
for (final StudentClearanceSNAL studentClearance : studentClearancess) {
我得到的错误信息是:
在第1行第2列处预期为 BEGIN_ARRAY,但实际为 BEGIN_OBJECT
英文:
Am trying to access my api that return an object with an array of data but i cant access am getting an error saying Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $ am using retrofit into my android application
Here is what the api returns when i call it
{
"students": [
{
"_id": "5e74775518b0f00c0123925b",
"registrationno": "IWR/D/2016/0024",
"firstname": "Mariam",
"lastname": "Wamigomba",
"amount": 10000,
"reason": "lost key ",
"__v": 0
}]}
here is my interface
public interface OcappJsonApiSNAL {
@GET("getStudents")
Call<List<StudentClearanceSNAL>> getStudents(@Header ("Authorization") String token);
}
On my java class
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)
{
Error Am Getting
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
答案1
得分: 1
Call<StudentClearanceSNALList> listCall = ocappJsonApi.getStudents("Bearer token");
Create new model:
class StudentClearanceSNALList {
@SerializedName("students") // it must match with your JSON object name
List<StudentClearanceSNAL> studentClearanceSNAL = new ArrayList<>();
// getter and setter
}
Then update your interface and class like the following code:
Interface:
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();
}
});
Why I suggest this is because your code expects a list, but you're passing an object, which is causing the issue. Please use the above code. I hope it helps you. Thanks.
英文:
Call<List<StudentClearanceSNAL>> listCall = ocappJsonApi.getStudents ("Bearer token");
Instead of map list externally you just create new model with list
Create new model :
Class StudentClearanceSNALList{
@SerializedName("students") // it’s must match with your Jason object name
List<StudentClearanceSNAL> studentClearanceSNAL = new Arraylist<>();
//getter and setter
}
Then update your interface and class like the following code:
Interface:
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 ();
Why I suggest this means it’s expect list but you pass object so it’s not accept so please use the above code.
I hope it’s helps you thanks
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论