我有一系列在onclick方法中以菊花链形式连接的AlertDialogs。在最后一个我需要发送一个URL调用并等待响应。一旦响应回来,我需要接收响应的消息并创建最后一个AlertDialog。问题是这适用于2.3 Android但由于REST http调用在UI线程上运行,因此Android 4.0的最新更改错误。所以我想把它转移到AsyncTask。我仔细阅读了教程,但没有找到我的具体困境。这是我的代码:如何使AlertDialog等待响应,同时等待显示ProgressDialog或Spinner?我正在寻找优雅的东西,其中AsyncTask是一个通用调用,并且可以在其他情况下用于REST调用。
public void onClick(View view) {
if(alert != null && alert.isShowing()) {
break;
}
alert = new AlertDialog.Builder(this)
.setTitle(getResources().getString(R.string.alertPingTitle1).toString())
.setMessage(getResources().getString(R.string.alertPingMessage1).toString())
.setPositiveButton(getResources().getString(R.string.alertPingPositive1).toString(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onClicked) {
log.info("Preventing multiple clicks to Ping Node Id");
return;
}
onClicked = true;
String returnMessage = "";
int iconId = 0;
try {
String url = String.format(getResources().getString(R.string.urlIdPing).toString(), getRestUrl(), getId() );
// This is where the Rest call will happen in an AsyncTask
AsyncRest rest = new AsyncRest( Preferences.this );
rest.execute( new RestObject(type,null,null,true,false) );
..........................
// This is the current state of the 2.3 code
JSONObject sent = RestRequest.sendPost(RestHttp.createHttpClient(), url, null, true);
if (sent != null && sent.getInt("response") == 200) {
returnMessage = "VALID REST call ";
iconId = android.R.drawable.ic_dialog_info;
} else {
if (sent != null) {
returnMessage = "INVALID REST response " + "\n\n Response code: " + sent.getInt("response");
} else {
returnMessage = "INVALID REST call.";
}
iconId = android.R.drawable.ic_dialog_alert;
}
} catch(JSONException e) {
returnMessage = "Errored in JSON payload.";
}
alert = new AlertDialog.Builder(SetPreferences.this)
.setTitle(getResources().getString(R.string.alertPingTitle2).toString())
.setMessage(returnMessage)
.setIcon(iconId)
.setPositiveButton(getResources().getString(R.string.alertOkay).toString(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
}
})
}).show();
onClicked = false;
}
答案 0 :(得分:1)
你并没有真正确定问题是什么,但我认为你不确定如何实现AsyncTask
。
查看AsyncTask
的开发者文档:http://developer.android.com/reference/android/os/AsyncTask.html
有一个例子,你可以在那里。基本上,您希望在AsyncTask的doInBackground()
内进行REST调用。然后,在调用完成后,您需要对UI进行的任何更改都将显示在onPostExecute()
。
编辑:更多细节
private class AsyncRest extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... url) {
// do your REST call here
return null;
}
protected void onPostExecute(Void nothing) {
// update your UI here
}
}
要执行,请致电new AsyncRest().execute(url);
要获取后台任务的结果,请在执行后调用get()。
AsyncRest rest = new AsyncRest( SetPreferences.this );
rest.execute( new RestObject(Constants.ROTypes.post,url,null,null,true,false) );
JSONObject sent = rest.get();
答案 1 :(得分:1)
使用AsyncTask
代码看起来像这样:
在你的活动中......
public MyActivity extends Activity implements AsyncRestCallback {
public void onClick(View view) {
if(alert != null && alert.isShowing()) {
break;
}
alert = new AlertDialog.Builder(this)
.setTitle(getResources().getString(R.string.alertPingTitle1).toString())
.setMessage(getResources().getString(R.string.alertPingMessage1).toString())
.setPositiveButton(getResources().getString(R.string.alertPingPositive1).toString(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (onClicked) {
log.info("Preventing multiple clicks to Ping Node Id");
return;
}
onClicked = true;
String returnMessage = "";
int iconId = 0;
String url = String.format(getResources().getString(R.string.urlIdPing).toString(), getRestUrl(), getId() );
AsyncRest rest = new AsyncRest().execute( url );
})
}).show();
onClicked = false;
}
@Override
public void finished(String returnMessage) {
alert = new AlertDialog.Builder(SetPreferences.this)
.setTitle(getResources().getString(R.string.alertPingTitle2).toString())
.setMessage(returnMessage)
.setIcon(iconId)
.setPositiveButton(getResources().getString(R.string.alertOkay).toString(),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).show();
}
}
RestAsync
public class AsyncRest extends AsyncTask<String, Integer, String> {
private AsyncRestCallback callback;
public AsyncRest(AsyncRestCallback callback) {
this.callback = callback;
}
@Override
protected String doInBackground(String... params) {
String returnMessage = "";
String url = params[0]
try {
JSONObject sent = RestRequest.sendPost(RestHttp.createHttpClient(), url, null, true);
if (sent != null && sent.getInt("response") == 200) {
returnMessage = "VALID REST call ";
iconId = android.R.drawable.ic_dialog_info;
}
else {
if (sent != null) {
returnMessage = "INVALID REST response " + "\n\n Response code: " + sent.getInt("response");
}
else {
returnMessage = "INVALID REST call.";
}
iconId = android.R.drawable.ic_dialog_alert;
}
} catch(JSONException e) {
returnMessage = "Errored in JSON payload.";
}
return returnMessage;
}
@Override
protected void onPostExecute(String returnMessage) {
callback.finished(returnMessage);
}
public interface AsyncRestCallback {
public void finished(String returnMessage);
}
}