How is it possible to have feeds from different Urls in the same ListView
android, feed, listview, url
Solution
I am exactly doing this in my app,
This is how i am doing this, I have created `AsyncTask` to parse RSS, and using a loop to pass different URLs to be parsed to `AysncTask`.
Here is what I mean,
This is the array of URL,
private String[] newsURLs = {
"url1", "url2", "url3" };
this is how I am executing them,
//GetNews is AsyncTask class
for (int i = 0; i < newsURLs.length; i++) {
GetNews blog = new GetNews(i);
blog.execute();
}
In the above code `i`is the URL number passed in as a constructor in `AsyncTask` class.
Here is my constructor in `AsyncTask` class,
int number;
public GetNews(int urlNumber) {
number = urlNumber;
}
and loading the URL in `AsyncTask`'s `doInBackground()` method this way,
URL feedURL = new URL(newsURLs[number]);
HttpURLConnection connection;
connection = (HttpURLConnection) feedURL.openConnection();
connection.setConnectTimeout(8000);
connection.connect();
After that, I am using `SAXParser` to parse `xml` based on my need.
Please note that if you want parallel execution, replace `blog.execute();` with
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);
Also note that,
When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. After HONEYCOMB, it is planned to change this back to a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.
DONUT is Android 1.6, HONEYCOMB is Android 3.0.
You may also execute the task based on what version you have,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
blog.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
blog.execute();
}
This is my partial code to show what I am doing,
// 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();
}
}
}
This is `AsyncTask` class (partial code),
private class GetNews extends
AsyncTask<Object, Void, Void> {
protected int number;
public GetNews(int urlNumber) {
number = urlNumber;
}
@Override
protected void doInBackground(
Object... arg0) {
try {
// Check if the feed is Live
URL feedURL = new URL(newsURLs[number]);
HttpURLConnection connection;
connection = (HttpURLConnection) feedURL.openConnection();
connection.setConnectTimeout(8000);
connection.connect();
Problem
With my current project I can take datas from a single Url and then display them in my Custom Listview. So is there a way I can get datas from more than one Url and then put in the same ListView even if the datas from each Url are called differently? This is my code that allows me to have the items from a 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; } } ```