gpt4 book ai didi

java - 带进度对话框的异步任务

转载 作者:行者123 更新时间:2023-12-01 12:50:49 26 4
gpt4 key购买 nike

我知道有很多关于此问题的答案,但我找不到我真正需要的:

1)当用户点击按钮时,显示一个进度对话框;

2) 执行类 AsyncTask 并等待答案(这是使用 HTTPUrlConnection 的响应);

3) 关闭进度对话框;

我尝试了很多东西,但进度对话框没有“出现”。我的代码:

   public class MainActivity extends Activity implements OnTaskCompleted{
..
private ProgressDialog progressDialog;
private Button btnLogin;
..

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnLogin = (Button) findViewById(R.id.btnLogin);

btnLogin.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
progressDialog = ProgressDialog.show(MainActivity.this,
"", "Scanning Please Wait", true);
try {
String param1 = "testParam1";
String param2 = "testParam2";
String response = new SyncHelper(MainActivity.this).execute("http://server.example.com/api", param1, param2).get(); //this way, my activity waits of the answer
Log.d(TAG, "Finished: " + response);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
// user didn't entered username or password
Toast.makeText(getApplicationContext(),
"Done",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
}
});
}

public void onTaskCompleted()
{
progressDialog.dismiss();
}



public class SyncHelper extends AsyncTask<Object, Void, String>
{
..
private OnTaskCompleted listener;
..
protected String doInBackground(Object... url) {
String response = "";
try {
response = getRequest((String) url[0],(String) url[1], (String) url[2]); //Here I make a HttpURLConnection
} catch (IOException e) {
e.printStackTrace();
}
return response;
}

@Override
protected void onPreExecute() {
}

protected void onPostExecute(String result) {
listener.onTaskCompleted();
}
}
public interface OnTaskCompleted{
void onTaskCompleted();
}

最佳答案

public class MainActivity extends Activity{
..

private Button btnLogin;
..

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btnLogin = (Button) findViewById(R.id.btnLogin);

btnLogin.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
try {
String param1 = "testParam1";
String param2 = "testParam2";
new SyncHelper(MainActivity.this).execute("http://server.example.com/api", param1, param2); //this way, my activity waits of the answer
Log.d(TAG, "Finished: " + response);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else {
// user didn't entered username or password
Toast.makeText(getApplicationContext(),
"Done",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
}
});
}





public class SyncHelper extends AsyncTask<String, Void, String>
{


..

Context context;
private ProgressDialog pd;
..

public SyncHelper (Context c)
{
context = c;
}


@Override
protected void onPreExecute() {
pd = new ProgressDialog(context);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
protected String doInBackground(String... url) {
String response = "";
try {
response = getRequest(url[0], url[1], url[2]); //Here I make a HttpURLConnection
} catch (IOException e) {
e.printStackTrace();
}
return response;
}


protected void onPostExecute(String result) {

// here you will be getting the response in String result.
if (pd.isShowing())
pd.dismiss();

}
}

当你使用get时,使用AsyncTask没有任何意义。因为 get() 会阻塞 UI 线程,也许这就是为什么看不到进度对话框的原因。如果您想将响应发送回MainActivity,请像使用beofre一样使用回调接口(interface)。

关于java - 带进度对话框的异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226260/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com