Java 如何创建异步任务以构建RSSReader
在投票之前。是的,我在问这个问题之前读过论坛 读了上面的那个,但我还是不明白 问题是: 我用Java编写了een RSSReader。这在控制台中非常有效,可以打印我想要的内容等。但在Android中它不起作用,因为它没有使用een异步任务。现在我从Google文档中了解到,有三种类型可以输入类似的任务。我不知道如何在代码中实现这一点。我是否需要创建一个单独的类,用AsyncTask扩展它,创建我的Reader的实例,并在它的doInBackground方法中调用我的Reader,或者我需要如何做到这一点 这是我的RSSReader的代码:Java 如何创建异步任务以构建RSSReader,java,android,asynchronous,android-asynctask,Java,Android,Asynchronous,Android Asynctask,在投票之前。是的,我在问这个问题之前读过论坛 读了上面的那个,但我还是不明白 问题是: 我用Java编写了een RSSReader。这在控制台中非常有效,可以打印我想要的内容等。但在Android中它不起作用,因为它没有使用een异步任务。现在我从Google文档中了解到,有三种类型可以输入类似的任务。我不知道如何在代码中实现这一点。我是否需要创建一个单独的类,用AsyncTask扩展它,创建我的Reader的实例,并在它的doInBackground方法中调用我的Reader,或者我需要如何
public class RSSReader {
//Lists to store headlines, descriptions & images
String url = "http://www.nu.nl/rss/Algemeen";
List<String> titleList;
List<String> descriptionList;
List<String> imageList;
public RSSReader(){
try {
titleList = readRSS(url, "<title>", "</title>");
descriptionList = listFilter(readRSS(url, "<description>", "</description>"), "&nbsp;", "");
imageList = readRSS(url, "<enclosure url \"", "\" length=\"0\" type=\"image/jpeg\"</enclosure>");
}
catch (IOException e){
}
}
public List<String> readRSS(String feedUrl, String openTag, String closeTag) throws IOException, MalformedURLException {
URL url = new URL(feedUrl);
BufferedReader reader= new BufferedReader(new InputStreamReader(url.openStream()));
String currentLine;
List<String> tempList = new ArrayList<String>();
while((currentLine = reader.readLine()) != null){
Integer tagEndIndex = 0;
Integer tagStartIndex = 0;
while (tagStartIndex >= 0){
tagStartIndex = currentLine.indexOf(openTag, tagEndIndex);
if(tagStartIndex >= 0){
tagEndIndex = currentLine.indexOf(closeTag, tagStartIndex);
tempList.add(currentLine.substring(tagStartIndex + openTag.length(), tagEndIndex) + "\n");
}
}
}
tempList.remove(0);
return tempList;
}
public List<String> getDesciptionList(){
return descriptionList;
}
public List<String> getTitleList(){
return titleList;
}
public List<String> getImageList(){
return imageList;
}
public List<String> listFilter(List<String> tempList, String require, String
replace){
//Creates new List
List<String> newList = new ArrayList<>();
//Loops through old list and checks for the 'require' variable
for(int i = 0; i < tempList.size(); i++){
if(tempList.get(i).contains(require)){
newList.add(tempList.get(i).replace(require, replace));
}
else{
newList.add(tempList.get(i));
}
}
return newList;
}
}
公共类RSSReader{
//用于存储标题、说明和图像的列表
字符串url=”http://www.nu.nl/rss/Algemeen";
列表标题列表;
列表描述列表;
列表图像列表;
公共RSSReader(){
试一试{
titleList=readRSS(url,“,”);
descriptionList=listFilter(readRSS(url,“,”),“&;nbsp;”,”);
imageList=readRSS(url,“你是对的,你需要Asynctask。但是这里解释太多了,这里已经解释得非常透彻了,所以你可能想看看:
您需要确保的是在doInBackground
中运行网络调用,您可以在onPreExcute
中操作UI,并在onpostExecute
中完成。有关更多详细信息,请访问链接。我假设您已经知道代码,因此doInBackground方法中应该是长期运行的代码,例如从internet/服务器等获取信息。然后,您可以返回一个包含成功或错误的字符串,该字符串将从onPostExecute方法中捕获,在该方法中,您可以随意处理结果
因此,我想说不需要新类,只需在这里扩展异步任务,实现我提到的两个方法,并在方法中调用您已经拥有的正确函数,只需对返回结果做一点更改。在rssrader\readRSS
中,您不需要选中templast.size()
别忘了加上
<uses-permission android:name="android.permission.INTERNET"/>
谢谢,这段代码给出了一个空指针异常。列表为空,但我在Intelij中测试了我的代码,我确信代码会填充列表
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new RssReaderAsyncTask(new RSSCallBack() {
@Override
public void success(RSSReader rssReader) {
// TODO That Should run on UI Thread if you update UI
// for example
final RSSReader reader = rssReader;
// you can use runOnUiThread or Handler update UI
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Toast
Toast.makeText(MainActivity.this, reader.getTitleList().toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void failed() {
// TODO That Should run on UI Thread if you update UI
Log.e("RSS", "failed");
}
}).execute("");
}
private class RssReaderAsyncTask extends AsyncTask<String, Integer, Integer> {
private RSSCallBack rssCallBack;
public RssReaderAsyncTask(RSSCallBack rssCallBack) {
this.rssCallBack = rssCallBack;
}
@Override
protected Integer doInBackground(String... params) {
// TODO
try {
RSSReader reader = new RSSReader();
rssCallBack.success(reader);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
rssCallBack.failed();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
rssCallBack.failed();
e.printStackTrace();
}
return null;
}
}
private interface RSSCallBack {
void success(RSSReader rssReader);
void failed();
}
public class RSSReader {
// Lists to store headlines, descriptions & images
String url = "http://www.nu.nl/rss/Algemeen";
List<String> titleList;
List<String> descriptionList;
List<String> imageList;
public RSSReader() throws MalformedURLException, IOException {
titleList = readRSS(url, "<title>", "</title>");
descriptionList = listFilter(readRSS(url, "<description>", "</description>"), "&nbsp;", "");
imageList = readRSS(url, "<enclosure url \"", "\" length=\"0\" type=\"image/jpeg\"</enclosure>");
}
public List<String> readRSS(String feedUrl, String openTag, String closeTag)
throws IOException, MalformedURLException {
URL url = new URL(feedUrl);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String currentLine;
List<String> tempList = new ArrayList<String>();
while ((currentLine = reader.readLine()) != null) {
Integer tagEndIndex = 0;
Integer tagStartIndex = 0;
while (tagStartIndex >= 0) {
tagStartIndex = currentLine.indexOf(openTag, tagEndIndex);
if (tagStartIndex >= 0) {
tagEndIndex = currentLine.indexOf(closeTag, tagStartIndex);
tempList.add(currentLine.substring(tagStartIndex + openTag.length(), tagEndIndex) + "\n");
}
}
}
if (tempList.size() > 0) {
//TODO you do not check it
tempList.remove(0);
}
return tempList;
}
public List<String> getDesciptionList() {
return descriptionList;
}
public List<String> getTitleList() {
return titleList;
}
public List<String> getImageList() {
return imageList;
}
public List<String> listFilter(List<String> tempList, String require, String replace) {
// Creates new List
List<String> newList = new ArrayList<String>();
// Loops through old list and checks for the 'require' variable
for (int i = 0; i < tempList.size(); i++) {
if (tempList.get(i).contains(require)) {
newList.add(tempList.get(i).replace(require, replace));
} else {
newList.add(tempList.get(i));
}
}
return newList;
}
}
}