1

我在 AsyncTask 中调用了 2 个 Web 服务方法。第一个已成功调用,但未调用第二个 Web 服务方法。知道如何在第一种方法之后调用第二种方法吗?

异步任务代码

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

4 回答 4

0

创建一个 AsyncTask 的实例,然后最终执行它。并且还缺少覆盖方法。

instance.execute();

并添加

@override before the post execute method.
于 2013-08-01T04:43:00.867 回答
0

需要在 onPostExecute 方法中添加@Override 并且不能在主线程上使用网络传输。onPostExecute 中的内容需要在另一个 AsyncTask 中完成。

来自Android 开发参考

网络操作可能涉及不可预知的延迟。为防止这导致糟糕的用户体验,请始终在与 UI 不同的线程上执行网络操作。AsyncTask 类提供了一种从 UI 线程触发新任务的最简单方法

于 2013-08-01T04:52:26.697 回答
0

@覆盖

protected void onPostExecute(字符串结果){

Log.i("AsynckTask",result);

}

如果您想咨询另一个 Web 服务,您需要其他 AsynkTask 记住在后台而不是在 UIThread 中建立的连接

@覆盖

protected void onPostExecute(字符串结果){

Log.i("AsynckTask",result);

//建立新连接

新的 AsynkTas().execute();

}

于 2013-08-01T04:59:37.530 回答
0
 @Override is missing before onPostExecute(String result), and check result getting null
于 2013-08-01T04:41:56.267 回答