1

我有一个 android 应用程序,它假设将事务保存在一个数据库中,并通过调用 2 个单独的 web 方法来更新另一个数据库中的余额。我的第一个 web 方法被调用,但第二个 web 方法没有被调用。

异步任务代码

  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;
             }
            }
          }
    }
}
4

1 回答 1

3

您应该在这里使用 AsyncTask 而不是 Thread,通过使用两个单独的 AsyncTask,您可以在第一个的 onPostExecute() 方法中调用第二个任务。

请检查:http: //developer.android.com/reference/android/os/AsyncTask.html

于 2013-07-31T05:31:41.587 回答