如何在同一ListView中使用来自不同Url的Feed

时间:2014-12-01 22:27:31

标签: android listview url feed

使用我当前的项目,我可以从单个Url中获取数据,然后在我的自定义列表视图中显示它们。 那么有没有一种方法可以从多个Url获取数据然后放入相同的ListView,即使每个Url的数据被调用不同? 这是我的代码,允许我从URL获取项目:

    public  class CLASS1 extends Fragment {

    private RSSFeed myRssFeed = null;

    public CLASS1()
    {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1, null);

        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        try {
            URL rssUrl = new URL("URL");
            SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
            SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
            XMLReader myXMLReader = mySAXParser.getXMLReader();
            RSSHandler myRSSHandler = new RSSHandler();
            myXMLReader.setContentHandler(myRSSHandler);
            InputSource myInputSource = new InputSource(rssUrl.openStream());
            myXMLReader.parse(myInputSource);

            myRssFeed = myRSSHandler.getFeed();

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        if (myRssFeed!=null)
        {
            ListView list = (ListView)view.findViewById(android.R.id.list);
            CustomList adapter = new CustomList(getActivity(),myRssFeed.getList());
            adapter.addAll();
            list.setAdapter(adapter);

        }
        else
            Toast.makeText(getActivity(), "Spiacente, connessione non disponibile!" +
                            " Prova più tardi.",
                    Toast.LENGTH_LONG).show();
        return view;
    }
}

1 个答案:

答案 0 :(得分:3)

我正在我的应用中这样做,

我就是这样做的,我创建了AsyncTask来解析RSS,并使用循环将不同的URL传递给AysncTask

这就是我的意思,

这是一个URL数组,

private String[] newsURLs = {
            "url1", "url2", "url3" };

这就是我执行它们的方式,

//GetNews is AsyncTask class
for (int i = 0; i < newsURLs.length; i++) {
                GetNews blog = new GetNews(i);
                blog.execute();
            }

在上面的代码中,i是在AsyncTask类中作为构造函数传入的URL编号。

这是AsyncTask类中的构造函数,

int number;
public GetNews(int urlNumber) {
            number = urlNumber;
        }

以这种方式加载AsyncTask的{​​{1}}方法中的网址,

doInBackground()

之后,我正在使用 URL feedURL = new URL(newsURLs[number]); HttpURLConnection connection; connection = (HttpURLConnection) feedURL.openConnection(); connection.setConnectTimeout(8000); connection.connect(); 根据我的需要解析SAXParser

请注意,如果您想并行执行,请替换xml

blog.execute();

另请注意,

  

首次引入时,AsyncTasks在单个后台线程上串行执行。启动
  与DONUT一起,这被改为一个允许多个线程的线程池   任务并行运作。   在HONEYCOMB之后,计划将其更改回单个线程   避免并行执行导致的常见应用程序错误。如果   你真的想要并行执行,你可以使用   executeOnExecutor(Executor,Params ...)此方法的版本   THREAD_POOL_EXECUTOR;但是,请参阅那里的评论以获得警告   它的用途。

DONUT是Android 1.6,HONEYCOMB是Android 3.0。

您也可以根据您的版本执行任务,

blog.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

// this type of executor uses the following params:
            //
            // private static final int CORE_POOL_SIZE = 5;
            // private static final int MAXIMUM_POOL_SIZE = 128;
            // private static final int KEEP_ALIVE = 1;
            //
            // private static final ThreadFactory sThreadFactory = new ThreadFactory() {
            //     private final AtomicInteger mCount = new AtomicInteger(1);
            //
            //     public Thread newThread(Runnable r) {
            //         return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());
            //     }
            // };
            //
            // private static final BlockingQueue<Runnable> sPoolWorkQueue =
            //        new LinkedBlockingQueue<Runnable>(10);

这是我的部分代码,用于展示我正在做的事情,

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    blog.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    blog.execute();
}

这是 // This is my Main class private String[] newsURLs = { "url1", "url2", "url3" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_myapp); for (int i = 0; i < newsURLs.length; i++) { GetNews blog = new GetNews(i); blog.execute(); } } } 类(部分代码),

AsyncTask