У меня есть сайт, с которого я с помощью retrofit делаю парсинг данных. Вот он
https://api.xn--41a.ws/api.php?method=search&q=%D0... С помощью
www.jsonschema2pojo.org я получил класс, который соответствует структуре данных одной песни. Когда я вставляю его в аргументы, он пишет мол Expected BEGIN_OBJECT but was BEGIN_ARRAY.
Код:
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();
}
});
}
}
GetSong.java:
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;
}
}
Link.java:
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