[英]communication between PHP and android
我正在開發一個Android應用程序,其中用戶將數據發送到PHP服務器。為此,我正在使用REST Web服務,我正在發送大量的數據,這需要花費近2分鍾的時間來執行,並且我希望用戶將數據發送到PHP服務器時,服務器應該發送成功消息給用戶和服務器在單獨的線程中執行數據。這怎么可能?
您可以使用AsyncTask執行繁重的操作而不會阻塞UI線程。
我相信這段代碼應該足以讓您入門。
private class MyHeavyTask extends AsyncTask<Void, Void, Boolean>{
// Perform initialization here.
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
progressDialog = new ProgressDialog(MyActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.show();
}
// If an error is occured executing doInBackground()
@Override
protected void onCancelled() {
if (progressDialog.isShowing())
progressDialog.dismiss();
// Show an alert box.
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setCancelable(false)
.setTitle("Error!")
.setMessage("Error in executing your command.")
.setInverseBackgroundForced(true)
.setPositiveButton("Dismiss", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
protected Boolean doInBackground(Void... arg0) {
if(!isCancelled()) {
// Perform heavy lifting here.
}
return true;
}
// After the request's performed. Here, hide the dialog boxes, inflate lists, etc.
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
您只需通過以下代碼行調用AsyncTask:
new MyHeavyTask().execute();
希望對您的問題有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.