【问题标题】:Async Task with multiple requests in androidandroid中具有多个请求的异步任务
【发布时间】:2016-04-21 21:14:24
【问题描述】:

我正在使用这个 asynctask 类来更新 Sql Server 上的两个不同的表,到目前为止这段代码工作正常我对这个类的更好和更充分的代码结构感兴趣,特别是在doinbackground() 可以调用多个 web 服务方法吗在一个线程中?有人可以建议我吗?

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



    }

【问题讨论】:

  • 对于Code Review,这听起来是个好问题。这是 StackExchange 网络上的另一个域,专门用于让程序员可以发布他们的代码并获得关于它的外观、执行方式等方面的反馈。在将您的问题发布到那里之前,请务必阅读他们的guide on asking questions。此外,如果您决定要在 Code Review 上提出问题,请务必先关闭此问题。谢谢!

标签: android android-asynctask httprequest android-ksoap2


【解决方案1】:

AsyncTask 应该只用于需要几秒钟的任务/操作。

AsyncTask 在单个后台线程上串行执行(来自 API 11)。所以长时间运行的工人可以阻止其他人。

检查其他一些gotchas

看看 HeandlerThread。

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2016-01-25
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    相关资源
    最近更新 更多