Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ using Retrofit

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

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ using Retrofit

问题

I'm here to help with the translation. Here's the translation of the code parts you provided:

我正在尝试使用 Retrofit 根据特定的 ID 从数据库获取数据但出现错误时调用 onError

> com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Students 类

public class Students implements Parcelable {
    private int id;
    @SerializedName("first_name")
    private String firstName;
    @SerializedName("last_name")
    private String lastName;
    private String course;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    private String score;

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.id);
        dest.writeString(this.firstName);
        dest.writeString(this.lastName);
        dest.writeString(this.course);
        dest.writeString(this.score);
    }

    public void readFromParcel(Parcel source) {
        this.id = source.readInt();
        this.firstName = source.readString();
        this.lastName = source.readString();
        this.course = source.readString();
        this.score = source.readString();
    }

    public Students() {
    }

    protected Students(Parcel in) {
        this.id = in.readInt();
        this.firstName = in.readString();
        this.lastName = in.readString();
        this.course = in.readString();
        this.score = in.readString();
    }

    public static final Parcelable.Creator<Students> CREATOR = new Parcelable.Creator<Students>() {
        @Override
        public Students createFromParcel(Parcel source) {
            return new Students(source);
        }

        @Override
        public Students[] newArray(int size) {
            return new Students[size];
        }
    };
}
这是我的 Retrofit 接口代码

public interface Api {

    @GET(url)
    Single<Students> getStudent(@Query("id") int id);
}
以及我的 MainActivity

Retrofit retrofit = new Retrofit.Builder()
                 .baseUrl(URL)
                    .addConverterFactory(GsonConverterFactory.create())
                        .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                            .build();
        
        Api api = retrofit.create(Api.class);
        
        api.getStudent(339).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<Students>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onSuccess(@NonNull Students students) {
                Log.i(TAG, "onSuccess: "+students);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.e(TAG, "onError: "+e );
            }
        });

如果您需要更多帮助或有其他问题,请随时提出。

英文:

I am Trying to get a data by a specific id from database using Retrofit but onError calls:

> com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

Students class:

public class Students implements Parcelable {
private int id;
@SerializedName(&quot;first_name&quot;)
private String firstName;
@SerializedName(&quot;last_name&quot;)
private String lastName;
private String course;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
private String score;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.firstName);
dest.writeString(this.lastName);
dest.writeString(this.course);
dest.writeString(this.score);
}
public void readFromParcel(Parcel source) {
this.id = source.readInt();
this.firstName = source.readString();
this.lastName = source.readString();
this.course = source.readString();
this.score = source.readString();
}
public Students() {
}
protected Students(Parcel in) {
this.id = in.readInt();
this.firstName = in.readString();
this.lastName = in.readString();
this.course = in.readString();
this.score = in.readString();
}
public static final Parcelable.Creator&lt;Students&gt; CREATOR = new Parcelable.Creator&lt;Students&gt;() {
@Override
public Students createFromParcel(Parcel source) {
return new Students(source);
}
@Override
public Students[] newArray(int size) {
return new Students[size];
}
};
}

and here is my retrofit interface code:

public interface Api {
@GET(url)
Single&lt;Students&gt; getStudent(@Query(&quot;id&quot;) int id);
}

and my MainActivity:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.build();
Api api = retrofit.create(Api.class);
api.getStudent(339).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver&lt;Students&gt;() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onSuccess(@NonNull Students students) {
Log.i(TAG, &quot;onSuccess: &quot;+students);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, &quot;onError: &quot;+e );
}
});

can you please tell me if I am doing something wrong here?

答案1

得分: 1

问题在于代码期望一个JSON对象(用花括号{}括起来),但实际遇到了一个JSON数组(用方括号[]括起来)。

问题出在你解析数据的方式上。

英文:

The issue is that the code is expecting a JSON object (enclosed within curly braces {}) but encountered a JSON array (enclosed within square brackets []) instead.

The issue is with the way you are parsing the data.

答案2

得分: 0

错误是因为您的API响应以数组形式开始:

[
{ "a": 1 }
]

它应该以对象形式开始,如下所示:

{ 
"a": 1
}
英文:

The error occur because your api response begin as an array:

[
{ &quot;a&quot;: 1 }
]

It should begin as an object, like as:

{ 
&quot;a&quot;: 1
}
</details>

huangapple
  • 本文由 发表于 2023年7月6日 18:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627767.html
匿名

发表评论

匿名网友

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

确定