我正在实施一个Android应用来加载RSS Feed。它完全正常,直到我决定添加一个新屏幕来显示两个按钮并尝试将String值传递给AsyncTask。这个想法是这样的:屏幕将显示两个按钮供用户选择。一旦用户点击其中一个按钮,它将调用AsyncTask以及字符串值(url)来读取RSS(url),然后rss feed由displayRSS(url)显示。 readRSS(url)将相应地加载RSS提要。
我的代码如下:
import java.util.List;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class RSSProActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rss);
startLoadRSS();
}
private void startLoadRSS(){
new RSS_Load().execute();
}
//
private void preReadRSS()
{
Toast.makeText(this, "Reading RSS, Please wait.", Toast.LENGTH_LONG).show();
}
private StringBuffer readRSS(String url)
{
//depends on the inputs, will set the String value accordingly. It should be "url" but using the default value at the moment.
String urlStr = "http://www.bom.gov.au/fwo/IDZ00059.warnings_vic.xml";
AndroidSAXFeedParser saxFeedParser = new AndroidSAXFeedParser(urlStr);
List<RSSItem> parse = saxFeedParser.parse();
StringBuffer sb = new StringBuffer();
sb.append(parse.size()+" items found");
for(RSSItem item:parse){
sb.append(item.getTitle()+"posted at "+item.getDate());
sb.append(item.getDescription());
}
return sb;
}
private void displayRSS()
{
StringBuffer sb = readRSS(url);
TextView textView = new TextView(this);
textView.setText(sb.toString());
setContentView(textView);
Toast.makeText(this, "SUCCESS", Toast.LENGTH_LONG).show();
}
//AsyncTask
public class RSS_Load extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
preReadRSS();
}
@Override
protected Void doInBackground(Void... arg0) {
readRSS();
// should be readRSS(url) here
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
//super.onProgressUpdate(values);
// updateProgressBar(values[0]);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// dismissProgressBar();
// should be displayRSS(url) here
displayRSS();
}
}
}
我正在尝试更改RSS_Load扩展AsyncTask中的值,但它给了我编译错误。所以我更改了受保护的Void doInBackground(String ... url),然后将url传递给readRSS(url),但它也不起作用。我该怎么做到这一点?
提前致谢!
答案 0 :(得分:4)
...意味着它实际上正在传递该类型的数组。因此,如果您将其更改为:
protected Void doInBackground(String... arg0)
你需要这样做:
@Override
protected Void doInBackground(String... arg0)
{
readRSS(arg0[0]);
// should be readRSS(url) here
return null;
}