Answer the question
In order to leave comments, you need to log in
Question about Retrofit: why does it say in the logs that Expected BEGIN_OBJECT but was BEGIN_ARRAY, although there is an object in the arguments?
I have a site from which I parse data using retrofit. Here it is https://api.xn--41a.ws/api.php?method=search&q=%D0... Using www.jsonschema2pojo.org I got a class that matches the data structure of one song. When I put it in arguments, it says Expected BEGIN_OBJECT but was BEGIN_ARRAY.
Code:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
EditText input;
Link link;
String url = "https://api.xn--41a.ws/";
Retrofit retrofit;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.text);
retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
link = retrofit.create(Link.class);
link.getSongs("Sting").enqueue(new Callback<GetSong>() {
@Override
public void onResponse(Call<GetSong> call, Response<GetSong> response) {
txt.setText("good");
}
@Override
public void onFailure(Call<GetSong> call, Throwable t) {
txt.setText("Fail");
t.printStackTrace();
}
});
}
}
public class GetSong {
@SerializedName("audio_id")
@Expose
private String audioId;
@SerializedName("artist")
@Expose
private String artist;
@SerializedName("title")
@Expose
private String title;
@SerializedName("mp3")
@Expose
private String mp3;
@SerializedName("duration")
@Expose
private Integer duration;
@SerializedName("img")
@Expose
private String img;
@SerializedName("hash")
@Expose
private String hash;
public String getAudioId() {
return audioId;
}
public void setAudioId(String audioId) {
this.audioId = audioId;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMp3() {
return mp3;
}
public void setMp3(String mp3) {
this.mp3 = mp3;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getHash() {
return hash;
}
public void setHash(String hash) {
this.hash = hash;
}
}
public interface Link {
@GET("api.php?method=search&key=5d2ef46e867e57510c93238c8bd5708a&v=3")
Call<GetSong> getSongs(@Query("q") String text);
}
W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
W/System.err: at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:150)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:224)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:386)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:215)
... 10 more
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question