以前也有類似的問題。但他們都沒有爲我工作。 我正在嘗試返回訂戶正在監聽的可觀察項。 每次用戶調用onError()方法時,都會調用「java.lang.IllegalStateException:預期的BEGIN_ARRAY,但BEGIN_OBJECT在第1行第2列路徑$」此錯誤。Retrofit Android:java.lang.IllegalStateException:期望BEGIN_ARRAY,但是BEGIN_OBJECT在第1行第2列路徑
這裏是我的代碼:
public Observable<List<String>> getPlaces() {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(Urls.BASE_URL)
.build();
RetroFitInterface service = retrofit.create(RetroFitInterface.class);
return service.getPlaces();
}
public interface RetroFitInterface {
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@GET(Urls.GET_POPULAR_LOCATIONS_URL)
Observable<List<String>> getPlaces();
}
這裏是一個JSON響應:
{
"locations": [
"location1",
"location2",
"location3",
"location4",
"location5",
"location6",
"location7",
],
"success": true
}
這是我如何訂閱用戶。
getPlaces().subscribeOn(Schedulers.io())
.subscribe(new Observer<List<String>>() {
@Override
public void onCompleted() {
Log.d(TAG, "Search result onCompleted()!");
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "Search result onError()! " + e.toString());
}
@Override
public void onNext(final List<String> result) {
Log.d(TAG, "This is never called.");
}
})
列表但是,響應是內容json數組的json對象。我認爲你需要使用pojo模式。使用GSON來對JSON進行對象數組列表映射。如果你沒有明白我的觀點,請告訴我。 –
你知道json基礎知識嗎?沒有!瞭解它,例外情況會很明顯。 – Selvin
@BhavdipPathar從你的幫助會更好。 – Kaustubh