2013-07-31 2 views
1

У меня есть приложение для Android, которое должно сохранять транзакцию в одной базе данных и обновлять баланс в другой базе данных, вызывая 2 отдельных веб-метода. Мой первый веб-метод вызывается, но второй веб-метод не вызывается.Как позвонить 2 веб-сервисам после нажатия кнопки в android?

AsyncTask код

public class Transaction extends AsyncTask<String,Void,Boolean> 
    { 
     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 Boolean doInBackground (String...url) 
      { 
      boolean good = true; 

      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) 
       { 
        System.out.println(line); 
       } 

      } catch (ClientProtocolException e) { 
        good = false; 
       } catch (IOException e) { 
        good = false; 
       } 
       return good; 
       } 

     protected void onPostExecute (Boolean good) 
      { 
      if(good == true) 
      { 
      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) { 
        good = false; 
      }catch (IOException e) { 
        good = false; 
      } 
      } 
      } 
    } 
} 
+2

использование AsSync задач и на пост выполнить вызов другого сервиса .. –

+0

использование AsyncTask для HttpClient. –

ответ

3

Вы должны использовать AsyncTask вместо Thread здесь, С помощью двух отдельных AsyncTask, Вы можете вызвать вторую задачу в методе первого своего onPostExecute().

Pls проверить это: http://developer.android.com/reference/android/os/AsyncTask.html

+0

Я все еще очень не понимаю, как его использовать. Заменить Thread trd = new Thread (new Runnable() onwards? –

+0

yes, вы должны заменить поток AysncTask: 'private class Task extends AsyncTask { protected Long doInBackground (xxx ... xxx) {// вызываем первую службу HTTP здесь} защищен недействительным onPostExecute (ххх ххх) {// вызываем другой сервис здесь} } ' – JaredLuo

+0

Тогда как я называю его по методу OnClick –

Смежные вопросы