应为 BEGIN_ARRAY 但在第 1 行第 1 列为 STRING 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);

        }

    });

}

我如何让这个错误消失?有没有特定的方法来处理字符串结果?


尚方宝剑之说
浏览 97回答 1
1回答

守着一只汪

你需要 APIService 接口是这样的import io.reactivex.Observable;import retrofit2.http.GET;public interface APIService{&nbsp; &nbsp; @GET("/api/?type=all-meat&paras=10&start-with-lorem=10")&nbsp; &nbsp; Observable<List<String>> getProductData();}然后在创建Retrofit实例的地方你会这样写Retrofit retrofit = new Retrofit.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .baseUrl("https://baconipsum.com/")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addConverterFactory(GsonConverterFactory.create(gson))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addCallAdapterFactory(RxJava2CallAdapterFactory.create())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();之后,无论您在哪里使用NewsFeed类,都将其替换为String.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java