2013-08-01 87 views
1

我有2個Web服務方法在AsyncTask中被調用。第一個被成功調用,但第二個Web服務方法未被調用。任何想法如何在第一個之後調用第二個方法?onPostExecute()方法不被執行

的AsyncTask代碼

public class Transaction extends AsyncTask<String,Void,String> 
    { 
     private String finalBalance1 = "", price1 = "", pName = "", pNric = "", pClass = "", sno = ""; 
     public Transaction(String price1, String pName, String pNric, String pClass, String sno, String finalBalance1) 
     { 
      super(); 
      this.finalBalance1 = Double.toString(finalBalance); 
      this.price1 = Double.toString(sPrice); 
      this.pName = sRA.studentName; 
      this.pNric = sRA.studentNric; 
      this.pClass = sRA.studentClass; 
      this.sno = logger.stallNo; 
     } 
     @Override 
      protected String doInBackground (String...url) 
      { 
      String result = ""; 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://152.226.152.175/NCO/WebService.asmx/InsertStudentTransaction"); 
      try 
      { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); 
       nameValuePairs.add(new BasicNameValuePair("NRIC", pNric)); 
       nameValuePairs.add(new BasicNameValuePair("Name", pName)); 
       nameValuePairs.add(new BasicNameValuePair("Class", pClass)); 
       nameValuePairs.add(new BasicNameValuePair("StallNo", sno)); 
       nameValuePairs.add(new BasicNameValuePair("AmountSpent", price1)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       InputStream in = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        result = line; 
       } 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
       } catch (IOException e) { 
       e.printStackTrace(); 
       } 
       return result; 
       } 

     protected void onPostExecute (String result) 
      { 
      if(result != null) 
      { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://152.226.152.175/NCO/WebService.asmx/UpdateParticulars"); 
      try 
      { 
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4); 
       nameValuePairs.add(new BasicNameValuePair("NRIC", pNric)); 
       nameValuePairs.add(new BasicNameValuePair("FixedAmount", finalBalance1)); 
       nameValuePairs.add(new BasicNameValuePair("Name", pName)); 
       nameValuePairs.add(new BasicNameValuePair("Class", pClass)); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       InputStream in = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) 
       { 
        System.out.println(line); 
       } 


      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      }catch (IOException e) { 
       e.printStackTrace(); 
      } 
      } 
      } 
    } 
} 
+1

@覆蓋它.. – KOTIOS

+0

哪裏'Transaction.execute();'? –

+0

我已經在我的按鈕的onclicklistener –

回答

0
@Override is missing before onPostExecute(String result), and check result getting null 
+0

我已經添加了它,但是現在有一個networkmainmainthreadexception –

+0

確保你必須在清單文件中寫入Intermet權限

0

創建的AsyncTask的實例,然後做finally.and也覆蓋方法缺失。

instance.execute(); 

,並添加

@override before the post execute method. 
+0

這是什麼用什麼做?該實例已被執行,因此doInBackground正在運行,並且onPostExecute將在被覆蓋時自動運行。 – ObieMD5

0

您需要@Override添加到onPostExecute方法,你也可以不使用主線程在網絡上傳輸。 onPostExecute內的東西需要在另一個AsyncTask中完成。

Android Dev Reference

網絡操作可能涉及不可預測的延遲。爲了防止這種 導致糟糕的用戶體驗,請始終在與UI不同的線程上執行網絡操作 。該的AsyncTask類提供的 從UI線程

0

@override

保護無效onPostExecute(字符串結果){

Log.i(「AsynckTask火了一個新任務的最簡單的方法之一「,結果);

}

,如果你想諮詢您需要其他AsynkTask切記不要在UIThread

@override

保護無效onPostExecute(字符串結果){

在後臺進行連接另一個Web服務Log.i( 「AsynckTask」,結果);

//創建新連接

new AsynkTas()。execute();

}