预期 BEGIN_ARRAY 但在第 1 行第 1 列 RetroFit 为 STRING
Expected BEGIN_ARRAY but was STRING at line 1 column 1 RetroFit
我有一个基本的 lorem ipsum 响应 json url 如下:
https://baconipsum.com/api/?type=all-meat¶s=10&start-with-lorem=10
在我的 APIService 接口中,我按如下方式调用它:
import io.reactivex.Observable;
import retrofit2.http.GET;
public interface APIService
{
@GET("application/json")
Observable<List<NewsFeed>> getProductData();
}
我使用响应数据的 NewsFeed 模型:
public class NewsFeed
{
private String feed;
public void setFeed(String feed)
{
this.feed = feed;
}
public String getFeed()
{
return feed;
}
}
然后我试图将 Url 中的数据传递到我的 recyclerview...
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
Observable<List<NewsFeed>> observable = apiService.getProductData().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
observable.subscribe(new Observer<List<NewsFeed>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<NewsFeed> newsFeeds)
{
newsFeedList = new ArrayList<>();
for(int i = 0; i < newsFeeds.size(); i++)
{
NewsFeed newsFeedModel = new NewsFeed();
newsFeedModel.setFeed(newsFeeds.get(i).getFeed());
newsFeedList.add(newsFeedModel);
}
mAdapter = new NewsAdapter(getActivity(), newsFeedList);
recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
}
@Override
public void onError(Throwable e) {
Log.e("Error", e.getMessage());
}
@Override
public void onComplete() {
}
});
}
如何消除此错误?是否有特定的方法来处理字符串结果?
您需要像这样的 APIService 接口
import io.reactivex.Observable;
import retrofit2.http.GET;
public interface APIService
{
@GET("/api/?type=all-meat¶s=10&start-with-lorem=10")
Observable<List<String>> getProductData();
}
然后在创建 Retrofit
实例的地方,您可以这样写
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://baconipsum.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
之后,无论您使用 NewsFeed
class,将其替换为 String
。
我有一个基本的 lorem ipsum 响应 json url 如下: https://baconipsum.com/api/?type=all-meat¶s=10&start-with-lorem=10
在我的 APIService 接口中,我按如下方式调用它:
import io.reactivex.Observable;
import retrofit2.http.GET;
public interface APIService
{
@GET("application/json")
Observable<List<NewsFeed>> getProductData();
}
我使用响应数据的 NewsFeed 模型:
public class NewsFeed
{
private String feed;
public void setFeed(String feed)
{
this.feed = feed;
}
public String getFeed()
{
return feed;
}
}
然后我试图将 Url 中的数据传递到我的 recyclerview...
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
Observable<List<NewsFeed>> observable = apiService.getProductData().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
observable.subscribe(new Observer<List<NewsFeed>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<NewsFeed> newsFeeds)
{
newsFeedList = new ArrayList<>();
for(int i = 0; i < newsFeeds.size(); i++)
{
NewsFeed newsFeedModel = new NewsFeed();
newsFeedModel.setFeed(newsFeeds.get(i).getFeed());
newsFeedList.add(newsFeedModel);
}
mAdapter = new NewsAdapter(getActivity(), newsFeedList);
recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
}
@Override
public void onError(Throwable e) {
Log.e("Error", e.getMessage());
}
@Override
public void onComplete() {
}
});
}
如何消除此错误?是否有特定的方法来处理字符串结果?
您需要像这样的 APIService 接口
import io.reactivex.Observable;
import retrofit2.http.GET;
public interface APIService
{
@GET("/api/?type=all-meat¶s=10&start-with-lorem=10")
Observable<List<String>> getProductData();
}
然后在创建 Retrofit
实例的地方,您可以这样写
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://baconipsum.com/")
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
之后,无论您使用 NewsFeed
class,将其替换为 String
。