我有一个 Asynctask 设置来从广播流中提取冰冷的元数据,如果我将 URL 放入代码中效果很好,但我希望 AlertDialog 从用户输入将 URL 传递给 Asynctask。我这样调用 AsyncTask :
MetadataTask1 metadataTask1 = new MetadataTask1();
metadataTask1.execute(uri);
当我将“uri”传递给 Asynctask 时,出现错误,
“AsyncTask 类型中的方法 execute(URL...) 不适用于参数 (Uri)”
我知道需要一个 URL,但我不知道该怎么做。我对 Android 很陌生,我相信这真的很简单而且很愚蠢。
我在这里发生了两件事:用户输入 URL 启动音乐服务并播放流,并且,相同的用户 URL 应该成为 AsyncTask 中的参数并开始提取冰冷的元数据。就像我说的,如果我在我的代码中手动输入一个 URL,一切都会正常工作,但当然不是这个主意。我的问题是: 1. 我如何处理用户输入(来自 AlertDialog)......我在哪里将它插入 AsyncTask 以及如何?2. doInBackground(URL... urls) 中的行呢?我确定这些是错误的:
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();
}
如果有人可以帮助我,谢谢!