2016-04-21 26 views
-2

的多個請求我使用這個類的AsyncTask更新SQL Server上的兩個不同的表至今代碼工作正常,我很感興趣,特別是在doinbackground()這個類的更多更好的和足夠的代碼結構是它好嗎在單個線程中調用多個webservices方法?任何人都可以建議我嗎?異步任務與機器人

 private class Update extends AsyncTask<Void, Void, Integer> { 
     private final int FAILED_INVALID_RESPONSE = 0; 
     private final int SUCCESS_GET_DATA = 1; 
     ProgressDialog progress; 
     private String _phoneno; 
     private String _ticket; 
     UpdateTicket(String phoneno,String ticket){ 
      _phoneno=phoneno; 
      _ticket=ticket; 
     } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      progress = ProgressDialog.show(XYZ.this, "", 
        "In Progress...", false); 
     } 

     @Override 
     protected Integer doInBackground(Void... params) { 
      method1(_phoneno); 
      return method2(_phoneno,_ticket); 
     } 
     @Override 
     protected void onPostExecute(Integer result) { 
      progress.dismiss(); 
      switch (result) { 
       case FAILED_INVALID_RESPONSE: 
        Toast.makeText(XYZ.this,"Please Check your Internet Connection.",Toast.LENGTH_SHORT).show(); 
        break; 
       case SUCCESS_GET_DATA: 
        Toast.makeText(XYZ.this, "Success!", Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
     int method1(String phoneno,String tickets) 
     { 
      final String methodname = "firstmethod"; 
      final String NAMESPACE ="http://tempuri.org/"; 
      final String URL="www.sampleurl.com"; 
      final String SOAP_ACTION="http://tempuri.org/firstmethod"; 
      int success=0; 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      SoapObject request = new SoapObject(NAMESPACE, methodname); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      request.addProperty("phoneno", phoneno); 
      request.addProperty("tickets", tickets); 
      envelope.setOutputSoapObject(request); 

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      try{ 
       androidHttpTransport.call(SOAP_ACTION,envelope); 
       SoapObject response = (SoapObject) envelope.bodyIn; 
      if(response!=null){ 
       success=1; 
      } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 

      } 

      return success; 
     } 
     int method2(String Phone) { 
      final String methodname = "secondmethod"; 
      final String NAMESPACE ="http://tempuri.org/"; 
      final String URL="www.sampleurl.com"; 
      final String SOAP_ACTION="http://tempuri.org/secondmethod"; 
      int success=0; 
      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 
      SoapObject request = new SoapObject(NAMESPACE, methodname); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      request.addProperty("phoneno", phoneno); 
      envelope.setOutputSoapObject(request); 
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      try{ 
       androidHttpTransport.call(SOAP_ACTION,envelope); 
       SoapObject response = (SoapObject) envelope.bodyIn; 
      if(response!=null){ 
       success=1; 
      } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 

      } 

      return success; 
     } 



    } 
+0

這聽起來像[代碼審查(https://codereview.stackexchange.com/)一個很好的問題。那是StackExchange網絡上的其他領域,特別是提出這樣程序員就可以發佈自己的代碼和它的外觀,它如何執行等,請務必閱讀[提出問題引導]他們(HTTP GET反饋://meta.codereview .stackexchange.com/questions/2436/how-to-get-the-best-value-out-of-code-review-questions-questions),然後在那裏發佈你的問題。另外,如果您決定在Code Review上提出您的問題,請務必先關閉此問題。謝謝! –

回答

1

AsyncTask只能用於需要幾秒鐘的任務/操作。

AsyncTasks在單個後臺線程(從API 11)串行地執行。如此長時間的跑步員可以阻止其他人。

檢查其他一些gotchas

看看HeandlerThread。