I have an Asynctask set up to pull icy metadata from shoutcast streams that works great if I put the URL in code, but i want an AlertDialog to pass a URL to the Asynctask from user input. I’m calling AsyncTask like this:
MetadataTask1 metadataTask1 = new MetadataTask1();
metadataTask1.execute(uri);
When I pass ‘uri’ to the Asynctask I get the error,
“The method execute(URL…) in the type
AsyncTask is not applicable for the arguments
(Uri)”
I know a URL is being expected but I can’t figure out how to do it. I’m very new to Android and I’m sure it’s something real simple and dumb.
I have 2 things happening here: User input URL starts the Music Service and plays the stream, and, the same user URL should become a param in the AsyncTask and start pulling icy metadata. Like I said everything works if I manually type a URL into my code but of course this isn’t the idea. My questions are:
1. How do I handle user input (from AlertDialog)….where do I plug it into AsyncTask and how?
2. What about the lines in doInBackground(URL… urls) I’m sure these are wrong:
try {
streamMeta.setStreamUrl(new URL(
"http://sfstream1.somafm.com:8880"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3 What to do about the Eclipse error when trying to add uri?
Here’s my code:
void showUrlDialog() {
class MetadataTask1 extends AsyncTask<URL, Void, IcyStreamMeta> {
@Override
protected IcyStreamMeta doInBackground(URL... urls)
{
try {
streamMeta.setStreamUrl(new URL(
"http://sfstream1.somafm.com:8880")); //I want a variable here from user input
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
streamMeta.refreshMeta();
Log.e("Retrieving MetaData", "Refreshed Metadata");
} catch (IOException e) {
Log.e(MetadataTask1.class.toString(), e.getMessage());
}
return streamMeta;
}
@Override
protected void onPostExecute(IcyStreamMeta result) {
//Toast toast = Toast.makeText(getBaseContext(),
// "Electropop and indie dance rock with sparkle and pop.",
// Toast.LENGTH_LONG);
//toast.setGravity(Gravity.BOTTOM, 0, 200);
//toast.show();
timer1.schedule(new TimerTask() {
public void run() {
if (timerIsOn1 == true) {
try {
title_artist = streamMeta.getArtist();
title_artist2 = streamMeta.getTitle();
Log.e("Retrieved title_artist", title_artist);
if (title_artist.length() > 0) {
runOnUiThread(new Runnable() {
public void run() {
Main.tx2.setText(title_artist);
Main.tx3.setText(title_artist2);
}
});
}
} catch (IOException e) {
Log.e(MetadataTask1.class.toString(),
e.getMessage());
}
try {
streamMeta.refreshMeta();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}, 0, 6000);
}
}
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("Manual Input");
alertBuilder.setMessage("URL (must be lowercase http://)");
final EditText input = new EditText(this);
alertBuilder.setView(input);
input.setHint("paste your URL here...");
alertBuilder.setPositiveButton("Play", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {
timerIsOn1 = true;
Main.tx1.setText("user station...");
Main.tx2.setText("");
Main.tx3.setText("");
// Send an intent with the URL of the song to play. This is expected by
// MusicService.
final Intent i = new Intent(MusicService.ACTION_URL);
Uri uri = Uri.parse(input.getText().toString());
i.setData(uri);
startService(i);
progressDialog = ProgressDialog.show(Main.this, "",
"connecting...");
progressDialog.setCancelable(true);
MetadataTask1 metadataTask1 = new MetadataTask1();
metadataTask1.execute(uri);
}
});
alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {}
});
alertBuilder.show();
}
Thank you if anyone can help me!
You declared the type of your AsyncTask as URL, Void, IcyStreamData, but then you’re trying to pass a URI (instead of a URL). You need to convert that URI to a URL. If you’re using java.net.URI then you can try uri.toURL();
As far as setting the url from the dialog, create your own custom dialog that can send the data from the edittext back to your activity. Then set an onDismissListener on the dialog and when it’s dismissed, check to see if you have a value passed back from the dialog and use that to execute the task. (of course you should/can use regex to validate the url)
I don’t see why you don’t just use a URL in the first place, but you can try using Uri.Builder and then call builder.build().toString() to have a String for a URL. Or just try uri.toString() and see if that gives you what you need, e.g. URL url = new URL(uri.toString());