2014-09-30 121 views
0

我正在開發一個android應用程序,用戶發送數據到PHP服務器。爲此,我正在使用其餘的web服務,我發送大量數據,這需要將近2分鐘才能執行,並且我想在用戶發送數據到PHP服務器,服務器應該發送回成功消息給用戶和服務器在單獨的線程中執行數據的執行。怎麼可能?PHP和android之間的通信

+0

使用JSON與'asynctask' – 2014-09-30 03:50:29

回答

2

您可以使用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(); 

希望它可以幫助您的問題。

+0

謝謝我知道解決方案,但給我服務器端解決方案如何服務器發送用戶成功消息,並在單獨的線程執行繁重的工作。 – 2014-09-30 05:25:55

+0

PHP的核心是一種單線程腳本語言。除非你依賴第三方解決方案,否則僅僅使用PHP執行多個線程是不可能的。 我不明白,爲什麼你甚至想要多線程解決方案,無論如何。 – 2014-09-30 05:45:22

+0

基本上我有一個700 lat lng的列表,我想要使用地理編碼器類的問題得到一個區域名稱反對那些緯度lng的問題是,當我得到區域名稱我的應用程序需要太多的時間來計算它這就是爲什麼我要計算它在服務器端不在移動端 – 2014-09-30 13:58:01