如何通过我的 API 调用使用 Retrofit 获取此对象

huangapple go评论73阅读模式
英文:

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

{
&quot;students&quot;: [
    {
        &quot;_id&quot;: &quot;5e74775518b0f00c0123925b&quot;,
        &quot;registrationno&quot;: &quot;IWR/D/2016/0024&quot;,
        &quot;firstname&quot;: &quot;Mariam&quot;,
        &quot;lastname&quot;: &quot;Wamigomba&quot;,
        &quot;amount&quot;: 10000,
        &quot;reason&quot;: &quot;lost key &quot;,
        &quot;__v&quot;: 0
    }]}

here is my interface

public interface OcappJsonApiSNAL {
    @GET(&quot;getStudents&quot;)
    Call&lt;List&lt;StudentClearanceSNAL&gt;&gt; getStudents(@Header (&quot;Authorization&quot;) String token);
}

On my java class

OcappJsonApiSNAL ocappJsonApi = retrofit.create (OcappJsonApiSNAL.class);
Call&lt;List&lt;StudentClearanceSNAL&gt;&gt; listCall = ocappJsonApi.getStudents (&quot;Bearer token&quot;);
listCall.enqueue (new Callback&lt;List&lt;StudentClearanceSNAL&gt;&gt; () {
    @Override
    public void onResponse(Call&lt;List&lt;StudentClearanceSNAL&gt;&gt; call, Response&lt;List&lt;StudentClearanceSNAL&gt;&gt; response) {
        if(!response.isSuccessful ()) {
            Toast.makeText (getActivity (),&quot;From OCApp &quot; + response.code (),Toast.LENGTH_LONG).show ();
            return;
        }
        List&lt;StudentClearanceSNAL&gt; 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&lt;List&lt;StudentClearanceSNAL&gt;&gt; listCall = ocappJsonApi.getStudents (&quot;Bearer token&quot;);

Instead of map list externally you just create new model with list

Create new model :

 Class StudentClearanceSNALList{
 @SerializedName(&quot;students&quot;) // it’s must match with your Jason object name
 List&lt;StudentClearanceSNAL&gt; studentClearanceSNAL = new Arraylist&lt;&gt;();
//getter and setter
}

Then update your interface and class like the following code:

Interface:

public interface OcappJsonApiSNAL {
    @GET(&quot;getStudents&quot;)
    Call&lt;StudentClearanceSNALList&gt; getStudents(@Header (&quot;Authorization&quot;) String token);
}

Class :

OcappJsonApiSNAL ocappJsonApi = retrofit.create (OcappJsonApiSNAL.class);
Call&lt;StudentClearanceSNALList&gt; listCall = ocappJsonApi.getStudents (&quot;Bearer token&quot;);
listCall.enqueue (new Callback&lt;StudentClearanceSNALList&gt; () {
    @Override
    public void onResponse(Call&lt;StudentClearanceSNALList&gt; call, Response&lt;StudentClearanceSNALList&gt; response) {
        if(!response.isSuccessful ()) {
            Toast.makeText (getActivity (),&quot;From OCApp &quot; + 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

huangapple
  • 本文由 发表于 2020年5月31日 01:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/62106559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定