在我当前的项目中,我可以从单个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; }}
解决方法:
我正在我的应用程序中这样做,
这就是我这样做的原因,我创建了AsyncTask来解析RSS,并使用循环传递不同的URL来解析为AysncTask.
这就是我的意思,
这是一个URL数组,
private String[] newsURLs = { "url1", "url2", "url3" };
这就是我执行它们的方式,
//GetNews is AsyncTask classfor (int i = 0; i < newsURLs.length; i++) { GetNews blog = new GetNews(i); blog.execute(); }
在上面的代码中,IIS是作为AsyncTask类中的构造函数传入的URL号.
这是我在AsyncTask类中的构造函数,
int number;public GetNews(int urlNumber) { number = urlNumber; }
并以这种方式在AsyncTask的doInBackground()方法中加载URL,
URL FeedURL = new URL(newsURLs[number]); httpURLConnection connection; connection = (httpURLConnection) FeedURL.openConnection(); connection.setConnectTimeout(8000); connection.connect();
之后,我使用SAXParser根据我的需要解析xml.
请注意,如果要并行执行,请替换blog.execute();
同
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);
另请注意,
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是AndroID 1.6,HONEYCOMB是AndroID 3.0.
您也可以根据自己的版本执行任务,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { blog.executeOnExecutor(AsyncTask.THREAD_POol_EXECUTOR);} else { blog.execute();}
这是我的部分代码来展示我在做什么,
// This is my Main classprivate 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类(部分代码),
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();
总结 以上是内存溢出为你收集整理的android – 如何在同一个ListView中使用来自不同Url的提要全部内容,希望文章能够帮你解决android – 如何在同一个ListView中使用来自不同Url的提要所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)