现在我正在创建一个RSS阅读器,主要是试图让项目的标题和描述显示在ListView上。我之前没有使用RSS数据测试它,并确认应用程序正确列出了我创建的项目。但是,在尝试使用RSS中的数据后,我遇到了检索实际RSS数据以及如何使用doinBackground方法的问题。
在阅读了有关doinBackground的Google文档后,我了解到它的类(Async)允许执行后台操作并将其结果显示在UI线程中。但是,我在提取RSS数据方面遇到了一些问题,而且doinBackground()如何适合我的代码。关于如何正确检索数据并有效使用doinbackground()的任何想法?
我遇到问题的代码类是Headlines和RSSManager。这是代码:
标题片段
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.net.MalformedURLException;
import java.net.URL;
public class Headlines extends Fragment {
EditText editText;
Button gobutton;
ListView listView;
public Headlines() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_headlines, container, false);
editText = (EditText)v.findViewById(R.id.urlText);
gobutton = (Button)v.findViewById(R.id.goButton);
listView = (ListView)v.findViewById(R.id.listView);
RSSFeedManager rfm = new RSSFeedManager();
News [] news = new News[100]; // i shouldnt have to set the size of the array here since I did it in getFeed() in RSSFeedManager.java
try {
news = rfm.getFeed(String.valueOf(new URL("http://rss.cnn.com/rss/cnn_world.rss")));
} catch (MalformedURLException e) {
e.printStackTrace();
}
RssAdapter adapter = new RssAdapter(this.getActivity(),news);
listView.setAdapter(adapter);
/*gobutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});*/
return v;
}
}
RSSFeedManager
import android.os.AsyncTask;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class RSSFeedManager extends AsyncTask<String,Void,String> {
public URL rssURL;
News[] articles;
public News[] getFeed(String url) {
try {
String strURL = url;
rssURL = new URL(url);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(rssURL.openStream());
//using Nodelist to get items within the rss, then creating
//a News array the same size of the amount of items within the rss
//then setting up a temporary "News" item which will be the temp object
//used for storing multiple objects that contain title and description
//of each item
NodeList items = doc.getElementsByTagName("item");
News[] articles = new News[items.getLength()];
News news = null;
//traverse through items and place the contents of each item within an RssItem object
//then add to it to the News Array
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
news.setTitle(getValue(item, "title"));
news.setDescription(getValue(item, "description"));
articles[i] = news;
}
} catch (Exception e) {
e.printStackTrace();
}
return articles;
}
public String getValue(Element parent, String nodeName) {
return parent.getElementsByTagName(nodeName).item(0).getFirstChild().getNodeValue();
}
@Override
protected String doInBackground(String... url) {
String rssURL = url[0];
URL urlTemp;
try {
//pulling the url from the params and converting it to type URL and then establishing a connection
urlTemp = new URL(rssURL);
HttpURLConnection urlConnection = (HttpURLConnection) urlTemp.openConnection();
urlConnection.connect();
/*
*im thinking i need to call the getFeed() method
*after establishing the httpurlconnection however
*I also thought I may just need to move the getFeed()
*code within doinBackground. Lost at this point due to the
* return types of getFeed and doinBackground
*/
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
答案 0 :(得分:0)
在挑选出每行代码之后,我想出了我的RSSFeedManager类,doInBackground()
方法和Headlines类的一些问题。
从RSSFeedManager开始,这里有一些问题。我将实例化的News [] articles
实例化为类变量,然后在getFeeds()
方法中重新定义它。显然这会导致一些问题并将articles
返回为null。我还删除了strURL
和rssURL
因为我正在以错误的方式解决这个问题。没有必要将URL传递给getFeeds()
,而是需要从URL传递XML。我还尝试了一些代码来使用News类的构造函数。
这是RSSFeedManager的固定代码:
public class RSSFeedManager{
News[] articles;
public News[] getFeed(String html) {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(html.getBytes()));
NodeList items = doc.getElementsByTagName("item");
articles = new News[items.getLength()];
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
News news = new News(getValue(item, "title"),getValue(item, "description").substring(0,100),"");
articles[i] = news;
}
} catch (Exception e) {
//e.printStackTrace();
Log.d("EXCEPTION PARSING",e.toString());
}
return articles;
}
public String getValue(Element parent, String nodeName) {
return parent.getElementsByTagName(nodeName).item(0).getFirstChild().getNodeValue();
}
}
正如我之前提到的,我意识到RSSFeedManager应该从URL而不是URL本身接收XML,并且新类是doInBackground()
应该发生的地方。本质上,带有Downloader类的doInBackground()
方法(扩展AsyncTask
)接收输入的URL,然后从该RSS url收集XML。
这是Downloader类
public class Downloader extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result ="";
try{
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while((line=reader.readLine())!= null){
result= result + line;
}
connection.disconnect();
} catch (Exception e) {
Log.e("Error fetching", e.toString());
}
return result;
}
}
所以在完成这两个课程之后,我知道我需要在Headlines课程中解决一些问题。我最初粗暴地强迫代码进行测试,以确保RSS源中的文章正确显示,然后他们做了,然后开始正确实现它。为了提醒人们,该程序的目的是通过将URL记录到editText
EditText
对象中来显示用户指定的RSS源中的文章。然后,用户按下类型为goButton
的{{1}},然后列出来自Button
的所有网址文章。我通过为editText
创建onClickListener
并创建goButton
和Downloader
类的对象来完成此操作,以便调用其方法来建立连接,检索URL的XML,然后解析它。
以下是Headline课程代码的片段,我可以将所有内容组合在一起。
RssFeedManager
所有这些变化都有助于解决问题并使我能够进一步完成此计划,并了解有关Android开发的更多信息。