简体   繁体   中英

calling .net web service with android using ksoap2

I am new in android programming (first project) and i need help on making my code to work... ok, so I have a method that would call the webservice and get the string from there. And it is working on the avd (on 2.2)

Here is the code:

public void NewCall(int Position)
{
    String SOAP_ACTION = "http://tempuri.org/";
    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "";
    String URL = "http://test.com/Service1.asmx?WSDL";

    if(Position == 0)
    {
        SOAP_ACTION += "Method1";
        METHOD_NAME = "Method1";
    }
    else if(Position == 1)
    {
        SOAP_ACTION += "Method2";
        METHOD_NAME = "Method2";
    }
    else if(Position == 2)
    {
        SOAP_ACTION += "Method3";
        METHOD_NAME = "Method3";
    }
    else if(Position == 3)
    {
        SOAP_ACTION += "Method4";
        METHOD_NAME = "Method4";
    }

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try 
    {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject result = (SoapObject)envelope.bodyIn;

        if(result != null)
        {
            String textRez = result.getProperty(0).toString();

            addRow(textRez); //method for inserting new row in the database

        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

But as i read, you can not do this in the 'main thread' with android 3.0+ , and as a solution I saw that you can do it with AsyncClass . And regarding this, i tried to combine examples i found, with my code, but it has not given me the result yet... What i think is that i need to parse the Position as Integer to the AssyncClass, do the code needed in doInBackground, and then i need to get the result as a String. I tried to compose something similar like this, but it gave me no success...

So if someone of you can help/guide me on solving this problem, i would be very grateful.

Thanks .

Edit: This is how i tried with using AsyncClass:

private class wsGet extends AsyncTask<String, Void, String>
{

    @Override
    protected String doInBackground(String... params) {
    int Position = Integer.parseInt(params[0]); 
    String SOAP_ACTION = "http://tempuri.org/";
    String NAMESPACE = "http://tempuri.org/";
    String METHOD_NAME = "";
    String URL = "http://test.com/Service1.asmx?WSDL";


    if(Position == 0)
    {
        SOAP_ACTION += "Method1";
        METHOD_NAME = "Method1";
    }
    else if(Position == 1)
    {
        SOAP_ACTION += "Method2";
        METHOD_NAME = "Method2";
    }
    else if(Position == 2)
    {
        SOAP_ACTION += "Method3";
        METHOD_NAME = "Method3";
    }
    else if(Position == 3)
    {
        SOAP_ACTION += "Method4";
        METHOD_NAME = "Method4";
    }

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);
        envelope.dotNet = true;

        try 
        {
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject result = (SoapObject)envelope.bodyIn;

            if(result != null)
            {
                tekstRez = result.getProperty(0).toString();

            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        return tekstRez;
    }

And then for calling this i tried with:

public void NewTenders(int Pos)
{
    String Position = String.valueOf(Pos);

    String[] params= {Position}; //to pass to doInBackground

    //Pass your args array and the current activity to the AsyncTask    

    String tekst = "";
    try {
        tekst = new wsGet().execute(params).get();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }addRow(tekst);

Though, it didn't work for me...

From android 3.0+ you cannot call web service from the main thread. write this whole code in the DoInBackground of your AsyncClass .

the AsyncClass cannot return value to the calling class. The return here passe the value to onPostExecute . use the folloing function in your AsyncClass

protected void onPostExecute(String result) 
{
//from here you have to pass your value to your main class
}

Or you can just add this line to the activity you're calling the webservice

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

Just after the setContentView() , and that should do the trick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM