princess_blackforest princess_blackforest - 5 months ago 16
Android Question

Android: Passing String parameters in AsyncTask?

I'm implementing an Android app to load RSS feed. It works perfectly fine until I decided to add a new screen for displaying two buttons and trying to pass a String value to the AsyncTask. The idea is this: The screen will display two buttons for the users to choose. Once the user has clicked on one of the buttons, it will invoke the AsyncTask along with the string value(url) to readRSS(url), then the rss feed is displayed by displayRSS(url). The readRSS(url) will load the RSS feed accordingly.

My codes are as below:

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();

}

}
}


I'm trying to change the value in RSS_Load extends AsyncTask but it gives me compile error. So I changed the protected Void doInBackground(String... url) then passing the url to readRSS(url) but it doesn't work either. How should I achieve this?

Thanks in advance!

Answer

The ... means that it's actually passing an array of that type. So if you change it to:

protected Void doInBackground(String... arg0)

You would need to do this:

@Override
protected Void doInBackground(String... arg0)
{
    readRSS(arg0[0]);
    // should be readRSS(url) here
    return null;
}