我有一个Asynctask设置来从shoutcast流中提取冰冷的元数据,如果我把URL放在代码中,那就很好用,但我希望AlertDialog将URL从用户输入传递给Asynctask。我正在调用AsyncTask:
MetadataTask1 metadataTask1 = new MetadataTask1();
metadataTask1.execute(uri);
当我将'uri'传递给Asynctask时,我得到了错误,
“类型中的方法execute(URL ...) AsyncTask不适用于参数 (URI)“
我知道网址正在被预期,但我无法弄清楚如何做到这一点。我对Android很陌生,我确信这是一件非常简单和愚蠢的事情。
我发生了两件事:用户输入URL启动音乐服务并播放流,同一用户URL应该成为AsyncTask中的一个参数并开始拉动冰元数据。就像我说的一切,如果我手动在我的代码中键入一个URL,但当然这不是主意。我的问题是: 1.我如何处理用户输入(来自AlertDialog)....我在哪里将其插入AsyncTask以及如何? 2. doInBackground中的行怎么样(URL ... url)我确定这些是错误的:
try {
streamMeta.setStreamUrl(new URL(
"http://sfstream1.somafm.com:8880"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3尝试添加uri时如何处理Eclipse错误?
这是我的代码:
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();
}
感谢有人可以帮助我!
答案 0 :(得分:1)
您将AsyncTask的类型声明为URL,Void,IcyStreamData,但之后您尝试传递URI(而不是URL)。您需要将该URI转换为URL。如果您正在使用java.net.URI,那么您可以尝试使用uri.toURL();
就在对话框中设置网址,创建自己的自定义对话框,可以将edittext中的数据发送回您的活动。然后在对话框上设置onDismissListener,当它被解除时,检查是否有从对话框传回的值并使用它来执行任务。 (当然你应该/可以使用正则表达式来验证网址)
我不明白你为什么不首先使用URL,但你可以尝试使用Uri.Builder然后调用builder.build()。toString()来获得一个URL的字符串。或者只是尝试uri.toString()并查看是否能满足您的需求,例如: URL url =新URL(uri.toString());
答案 1 :(得分:0)
以下是我如何使用它...我希望这可以节省其他时间...
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);
alertBuilder.setPositiveButton("Play", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {
//do stuff here
从用户输入中获取Uri:
Uri uri = Uri.parse(input.getText().toString());
需要将用户输入转换为URL:
URL url = null;
try {
url = new URL(uri.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后,在doInBackground中:
@Override
protected IcyStreamMeta doInBackground(URL... urls)
{
try
{
streamMeta.refreshMeta();
Log.e("Retrieving MetaData","Refreshed Metadata");
}
catch (IOException e)
{
Log.e(MetadataTask1.class.toString(), e.getMessage());
}
return streamMeta;
}
在传入URL时调用AsyncTask:
streamMeta.setStreamUrl(url);
MetadataTask1 metadataTask1 = new MetadataTask1();
metadataTask1.execute(url);