从execute()。get()开始解决

时间:2017-10-25 05:45:41

标签: android android-asynctask

以下是现有的片段----- 这部分代码来自我的活动类,我调用异步任务,由于execute()。get()我无法显示进度对话框,代码的一部分在多个活动中调用超过100次,只有旁路是我可以从主Async类本身。我已经尝试过回调,UpdateProgess方法。没有多大帮助。请帮忙,如果我可以做任何事情从异步类本身推Ui Dialog

 ArrayList<SoapObject> RecivedSoapObject;
 ArrayList<String> st_communityname ,st_communitycode

  try {
            RecivedSoapObject = new AsyncTask_Class(HouseBasicInfo_BasicSubActivity.this, "community_mst").execute().get();
            st_communityname = Utils.getNames(RecivedSoapObject, "value", "Please select Community");
            st_communitycode = Utils.getNames(RecivedSoapObject, "code", "Please select condition");

        } catch (InterruptedException ee) {
            ee.printStackTrace();
        } catch (ExecutionException ee) {
            ee.printStackTrace();
        }

AsyncTask类

public class AsyncTask_Class extends AsyncTask<Void, Void, ArrayList<SoapObject>> {
    String tablename;
    Activity activity;
    SoapObject request;
    Object resultAsObj;
    ArrayList<SoapObject> dataList;

    private static String SOAP_ACTION = null;

    int size = 0;
    Context mContext;


    ProgressDialog progressDialogView;

    public AsyncTask_Class(Context context, String tablename) {
        super();
        mContext = context;
        this.tablename = tablename;
    }

    public void onPreExecute() {
        super.onPreExecute();
////// this is not getting executed in UI thread
        progressDialogView = new ProgressDialog(mContext);
        progressDialogView.setMessage("Loading..");
        progressDialogView.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialogView.setCancelable(false);
        progressDialogView.show();


    }

    protected ArrayList doInBackground(Void... voids) {


        String WSDL_TARGET_NAMESPACE = "http://antydidayaservices.nic.com";

        String OPERATION_NAME = "getmaster";

        SOAP_ACTION = WSDL_TARGET_NAMESPACE + "/" + OPERATION_NAME;

        request = new SoapObject(WSDL_TARGET_NAMESPACE, OPERATION_NAME);

        request.addProperty("tablename", tablename);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);


        if (Utils.showLogs == 0) {
            Log.e(">>>>>>>>>>>>>>>>>>>>>1", request + "");
        }
        //HttpTransportSE httpTransport = new HttpTransportSE("http://10.160.2.163:8081/AntydodayaIndicatiorServices/services/GetMaster?wsdl");
        HttpTransportSE httpTransport = new HttpTransportSE(Utils.urlmain + Utils.master);

        try {
            httpTransport.call(SOAP_ACTION, envelope);
            resultAsObj = envelope.bodyIn;
        } catch (Exception e) {
            if (Utils.showLogs == 0) {
                Log.e(">>>>>>>>>>>>>>>>>>>>>>2", e.toString());
            }

        } finally {
            progressDialogView.dismiss();
        }

        if (Utils.showLogs == 0) {
            Log.e(">>>>>>>>>>>>>>>>>>>>>>3", resultAsObj + "");
        }

        dataList = new ArrayList<SoapObject>();
        if (((SoapObject) resultAsObj).getPropertyCount() > 0) {

            for (int i = 0; i < ((SoapObject) resultAsObj)
                    .getPropertyCount(); i++) {

                SoapObject soapObject = (SoapObject) ((SoapObject) resultAsObj)
                        .getProperty(i);
                dataList.add(soapObject);
            }
            size = dataList.size();
        }

        return dataList;

    }


}

1 个答案:

答案 0 :(得分:0)

只需删除execute()。get并从onPostExecute()方法获取结果。如果你的asynctask在其他单独的类中,那么使用接口来获取数据。