通过改造解析多个URL

时间:2018-02-01 13:05:44

标签: java android xml rss retrofit2

构建新闻Feed应用,目前正在使用改造来获取和解析RSS源。虽然我必须创建多个改装对象并调用才能在一个活动中解析多个URL。

       //initialising retrofit
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(UrlsList.getBaseUrlsList().get(7))
            //converter XML
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

    FeedChannelAPI feed = retrofit.create(FeedChannelAPI.class);

    Call<RssFeed> call = feed.getFeed(UrlsList.getRssUrlsList().get(7));

    //starting callbacks
    call.enqueue(new Callback<RssFeed>() {
        @Override
        public void onResponse(Call<RssFeed> call, Response<RssFeed> response) {
            Log.d(TAG, "onResponse: feed: " + response.body().toString());

            List<Item> mItems = response.body().getmChannel().getItems();

            Log.d(TAG, "onResponse: Server Response: " + response.toString());

            //initialise arraylist to add news to the class
            for (int i = 0; i < mItems.size(); i++) {
                XmlExtraction extractVerge = new XmlExtraction(mItems.get(i).getDescription(), "src=");
                List<String> postContent = extractVerge.start();
                news.add(new NewsClass(
                        mItems.get(i).getTitle(),
                        mItems.get(i).getPubDate(),
                        mItems.get(i).getLink(),
                        postContent.get(0),
                        response.body().getmChannel().getTitle()

                ));

            }

        }

        @Override
        public void onFailure(Call<RssFeed> call, Throwable t) {
            Log.e(TAG, "onFailure: Unable to retrieve RSS: " + t.getMessage());

        }

    });

    retrofit = new Retrofit.Builder()
            .baseUrl(UrlsList.getBaseUrlsList().get(13))
            //converter XML
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();

    feed = retrofit.create(FeedChannelAPI.class);

    call = feed.getFeed(UrlsList.getRssUrlsList().get(13));

    //starting callbacks
    call.enqueue(new Callback<RssFeed>() {
        @Override
        public void onResponse(Call<RssFeed> call, Response<RssFeed> response) {
            Log.d(TAG, "onResponse: feed: " + response.body().toString());

            List<Item> mItems = response.body().getmChannel().getItems();

            Log.d(TAG, "onResponse: Server Response: " + response.toString());

            //initialise arraylist to add news to the class
            for (int i = 0; i < mItems.size(); i++) {
                XmlExtraction extractVerge = new XmlExtraction(mItems.get(i).getDescription(), "img src=");
                List<String> postContent = extractVerge.start();
                news.add(new NewsClass(
                        mItems.get(i).getTitle(),
                        mItems.get(i).getPubDate(),
                        mItems.get(i).getLink(),
                        postContent.get(0),
                        response.body().getmChannel().getTitle()

                ));

            }

        }

        @Override
        public void onFailure(Call<RssFeed> call, Throwable t) {
            Log.e(TAG, "onFailure: Unable to retrieve RSS: " + t.getMessage());

        }

    });

我一直想知道是否可以解析多个基本URL。 目前我的界面如下所示

public interface FeedChannelAPI {

@GET
Call<RssFeed> getFeed(@Url String url);}

是否有可能作为参数进行解析List<String>我已尝试但应用程序仍然崩溃。总的来说,我想要实现的是能够使用一个改进对象和一个调用解析多个URL。有什么建议吗?

0 个答案:

没有答案