简体   繁体   中英

Android kSOAP web service problem

I'm trying to invoke a public web service (w3schools.com/webservices/tempconvert.asmx) via kSOAP (downloaded and included the ".jar" directly from Google).

Here's my code:

// declarations

private static final String NAMESPACE = "http://tempuri.org/" ;
private static final String METHOD_NAME = "CelsiusToFahrenheit";
private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private static final String URL = "http://216.128.29.26/webservices/tempconvert.asmx";

// code

try
{
   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
   HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

   request.addProperty("Celsius", "32");
   envelope.setOutputSoapObject(request);
   androidHttpTransport.call(SOAP_ACTION, envelope);

   Object result = envelope.getResponse();
}
catch(Exception e)
{
   e.printStackTrace();
}

Also, "AndroidManifest.xml" includes the permission to access the internet:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Also, my AVD can access the internet (tried it via the Browser app).

Can someone please help me? I've been trying this for about 6 hours now, and am still failing.

Thank you!

The problem is that you are doing a network call on the main thread and in API level 12 this is not allowed anymore. You just have to move the ksoap2 call to an AsyncTask or so. This problem would occur with anything doing network access by the way. It is an enforced strict mode check.

seems like your doing the same tutorial as i am. http://www.vimeo.com/9633556

it seems like you are missing envelope.dotNet = true;

Not quite. I had it before (tried at least 5 different approaches). It didn't help. It was the API version. I had 12. It didn't work. I found somewhere that kSOAP works only with 8 and lower. Go figure:)

I hope everyone who encountered this problem, will find this before they'll loose too much time with the 12 API.

i just tried my own code (which only differs in the webservice its trying con use) with api version 10 and ksoap2 and it still works as expected.

            private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
            private static final String METHOD_NAME = "CelsiusToFahrenheit";
            private static final String NAMESPACE = "http://tempuri.org/";
            private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
            System.out.println("--==Creating SOAP object==--");
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("Celsius", "32");
            System.out.println("--==Finished Creating SOAP object==--");

            System.out.println("--==Creating SOAP envelope==--");
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(request);
            System.out.println("--==Finished Creating SOAP envelope==--");

            HttpTransportSE aht = new HttpTransportSE(URL);
            // aht.debug = true;

            try {
                System.out.println("--==Creating SOAP call==--");
                aht.call(SOAP_ACTION, soapEnvelope);
                System.out.println("test");

                SoapPrimitive response = (SoapPrimitive) soapEnvelope.getResponse();
                System.out.println(response.toString());
                tableData.addView(resultSoap);
                System.out.println("--==Finished Creating SOAP call==--");
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (aht.debug == true) {
                System.out.println(aht.responseDump);
            }
package com.soap;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.AndroidHttpTransport;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TextView;
    public class SoapsActivity extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.tvsoap);
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("Celsius", "32");
        SoapSerializationEnvelope evenlop = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        evenlop.dotNet = true;
        evenlop.setOutputSoapObject(request);
        AndroidHttpTransport aht = new AndroidHttpTransport(URL);
        try {
            aht.call(SOAP_ACTION, evenlop);
            SoapPrimitive resultString = (SoapPrimitive) evenlop.getResponse();
            tv.setText("ststus :" + resultString.toString());
        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

}

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