如何将方法传递给android

时间:2016-05-27 21:39:36

标签: android

目前我正在尝试在Android中使用kso​​ap库在.net中使用Web服务的应用程序,我想要做的是拥有不同的方法并将它们传递给我的线程类,这样我就不会有多少线程我呼叫的每个Web服务的类。提前知道,这是我的示例代码

public boolean getAllAcounts()
    {
        Boolean connect = true;
        try
        {
            SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME1);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE transportSE = new HttpTransportSE(URL);
            transportSE.call(SOAP_ACTION1,envelope);

            SoapObject response = (SoapObject)envelope.getResponse();
            accountDetail = new String[response.getPropertyCount()][4];
            accounts = new String[response.getPropertyCount()];
            list = new ArrayList<>();

            if(response.getPropertyCount() > 0)
            {

                int y = 0;
                for(int i=0; i< response.getPropertyCount();i++)
                {
                    SoapObject xmlData = (SoapObject)response.getProperty(i);

                    //fill account list array

                    accountDetail[i][y + 0] = xmlData.getProperty(1).toString();
                    accountDetail[i][y + 1] = xmlData.getProperty(2).toString();
                    accountDetail[i][y + 2] = xmlData.getProperty(3).toString();
                    accountDetail[i][y + 3] = xmlData.getProperty(4).toString();

                    HashMap<String,String> temp = new HashMap<String,String>();
                    temp.put(AccountColumns.FIRST_COLUMN,xmlData.getProperty(1).toString());
                    temp.put(AccountColumns.SECOND_COLUMN,xmlData.getProperty(2).toString());
                    temp.put(AccountColumns.THIRD_COLUMN,xmlData.getProperty(3).toString());
                    temp.put(AccountColumns.FOURTH_COLUMN,xmlData.getProperty(4).toString());
                    list.add(temp);

                    y = 0;

                }
            }



        }
        catch (IOException e)
        {
            e.printStackTrace();
            connect = false;
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
            connect = false;
        }
        return connect;
    }

    public void fillListByAllAcount()
    {

        if(!list.isEmpty())
        {
            ListViewAdapter adapter = new ListViewAdapter(getActivity(),list);
            accountList.setAdapter(adapter);
        }
        else
        {
            Toast.makeText(getContext(),"Unable to get accounts make sure web service is running",Toast.LENGTH_SHORT).show();
        }



    }

   class AllAcounts extends AsyncTask<String,String,String>{


       @Override
       protected void onPreExecute() {
           //super.onPreExecute();
           dialog = new ProgressDialog(getContext());
           dialog.setMessage("Connecting...");
           dialog.setIndeterminate(false);
           dialog.setCancelable(false);
           dialog.show();

       }

       @Override
       protected String doInBackground(String... params) {
           if(getAllAcounts()){
               return "ok";
           }
           else{
               return "err";
           }


       }

       @Override
       protected void onPostExecute(String s) {
           //super.onPostExecute(s);
           dialog.dismiss();
           if(s.equals("ok")){
               //Log.e("ok",s);
               fillListByAllAcount();
           }
           else
           {
               Toast.makeText(getContext(),"Unable to connect to web service",Toast.LENGTH_SHORT).show();
           }
       }
   }

0 个答案:

没有答案