0

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!

Alex
  • 1,011
  • 2
  • 16
  • 27

5 Answers5

5

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.

Manfred Moser
  • 29,539
  • 13
  • 92
  • 123
  • I am doing the web service call in 'AsyncTask' . But getting Expected start_tag .....blah blah... Any suggestion . PS: a – iDroid Jun 29 '12 at 13:01
  • You need to debug to check what your response dump is. Look for details on the ksoap2-android wiki. – Manfred Moser Jun 29 '12 at 18:43
  • My response dump after caching the XMLPullParserException is a HTML String , which has the error description "CACHE ACCESS DENIED". Since it's a HTML response not SOAP XML Response. So The parser of KSOAP looking for "Start Tag of XML SOAP Response" and it's not found there , so triggered the exception "Expected START_TAG ...." – iDroid Jun 29 '12 at 20:30
  • Well.. your server should not throw a html response error but a soapfault... fix your server or catch the exception with that problem in your android code looking for that specific error message or so. – Manfred Moser Jun 30 '12 at 04:39
  • Hey, Thanks for the attention dude, I just fixed this issue by setting the PROXY in Application using java.net.Proxy instance. – iDroid Jul 01 '12 at 17:49
1

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

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

GriffiN
  • 84
  • 10
  • 1
    are you using ksoap2 or ksoap2-android? ksoap2-android can be downloaded from http://code.google.com/p/ksoap2-android/ – GriffiN Jul 12 '11 at 12:51
  • Thank you @GriffiN. now it's working.before I used this http://code.google.com/p/ksoap2-android/wiki/HowToUse?tm=2 ksoap2 but it's version not working in my project. But When I add your jar link , my project working. Thank you. – Erhan Demirci Dec 19 '12 at 09:37
0

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.

Alex
  • 1,011
  • 2
  • 16
  • 27
  • 2
    This is completely wrong. ksoap2-android works just fine up to Android 4.0. The problem is that you were trying network access on the UI thread like I stated in my answer and that is not allowed anymore by the Android framework itself from API level 12 and up. – Manfred Moser Nov 03 '11 at 16:07
0

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);
            }
GriffiN
  • 84
  • 10
  • Haven't tried it with 10. Tried it with 12 and it didn't work. Tried it with 8 and it worked. Try with 11 or 12... – Alex Jul 15 '11 at 14:17
  • Btw, we have the save service (I just translared w3schools.com to IP, cause I found a suggestion this might be the cause of my problems) :) – Alex Jul 15 '11 at 14:20
0
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());
        }

    }

}
jayesh kavathiya
  • 3,531
  • 2
  • 22
  • 25