我需要使用不同的 url 调用 asynctask。我有两个按钮,它们使用不同的 url 向服务器发送 http post,现在我可以使用一个 url 调用 asynctask 但如何使用不同的 url 调用相同的函数.. 意思是按钮 1 下载 url 编号 1 和按钮 2 下载 url 编号 2
class DownloadFont extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(MainAc.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("download"));
progressDialog.setTitle("wait"));
progressDialog.show();
}
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(progress[0]);
String info_download = "";
if (progress[0] > 99) {
download = (getResources().getString("dff));
} else {
download = (getResources().getString(R.string.download)) + progress[0] + "%";
}
progressDialog.setMessage(download);
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
if (result) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.done), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.error), Toast.LENGTH_SHORT).show();
}
}
@Override
protected Boolean doInBackground(String... arg0) {
Boolean flag = false;
try {
URL url = new URL("www.test_sweb.com/new.mp3");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
最佳答案
在 doInBackground 中:
@Override
protected Boolean doInBackground(String... arg) {
Boolean flag = false;
try {
URL url = new URL(arg[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
在您的 Activity 中或您想要开始任务的位置(onClick):
string URL = "www.test_sweb.com/new.mp3";//Or the url you need
new DownloadFont().execute(URL)
关于java - 如何使用两个 url 调用异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49389059/