Android kSOAP web service problem

2019-09-03 10:05发布

问题:

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!

回答1:

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.



回答2:

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

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



回答3:

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.



回答4:

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


回答5:

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

    }

}