views:

13325

answers:

5

Hai to All,I have a problem while calling the webservice,i have a .NET web service in the server and i am using KSOAP2(ksoap2-j2se-full-2.1.2) in android.While running the program i got an runtime Exception like "org.ksoap2.serialization.SoapPrimitive". I dont know what to do.Here is my code.

package projects.ksoap2sample;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.*;
import android.os.*;
import android.widget.TextView;

public class ksoap2sample extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String METHOD_NAME = "HelloWorld";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx";
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text1);

       try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            //request.addProperty("prop1", "myprop");

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();


           String[] results = (String[])  result;
            tv.setText( ""+results[0]); 
        } catch (Exception e) {
            tv.setText(e.getMessage());
            }
    }
}

May be my code is wrong.please help me.

Regards Rajapandian

A: 

Couple of months ago I have written an article about similar thing: Communication Between WCF Service and Android Client

niko
Good post, but he was interested in SOAP, not REST.
John Saunders
A: 

How does your .net Webservice look like?

I had the same effect using ksoap 2.3 from code.google.com
I followed the tutorial on codeproject.com (which is great btw.)

and everytime I used

    Integer result = (Integer)envelope.getResponse();

to get the result of a my webservice (regardless of the type, I tried Object, String, int) I ran into the org.ksoap2.serialization.SoapPrimitive exception.

I found a solution/workaround. The first thing I had to do was to remove the "SoapRpcMethod() attribute from my webservice methods.

    [SoapRpcMethod(), WebMethod]
    public Object GetInteger1(int i)
    {
        // android device will throw exception
        return 0;
    }

    [WebMethod]
    public Object GetInteger2(int i)
    {
        // android device will get the value
        return 0;
    }

then I changed my android code to:

    SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

However, I get a SoapPrimitive object, which has a "value" filed that is private. Luckily the value is passed through toString() method, so I use Integer.parseInt(result.toString()) to get my value, which is enough for me, cause I don't have any complex types that I need to get from my webservice.

Here is the full source:

private static final String SOAP_ACTION = "http://tempuri.org/GetInteger2";
private static final String METHOD_NAME = "GetInteger2";
private static final String NAMESPACE = "http://tempuri.org/";    
private static final String URL = "http://10.0.2.2:4711/Service1.asmx";

public int GetInteger2() throws IOException, XmlPullParserException {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo pi = new PropertyInfo();
    pi.setName("i");
    pi.setValue(123);            
    request.addProperty(pi);

    SoapSerializationEnvelope envelope = 
        new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);

    SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
    return Integer.parseInt(result.toString());

}
SchlaWiener
A: 

Hi, I'm new to Android and this sample code was very helpfull. Calling the Soap methode and getting the response is now ok. But I'v the following exception at the very end when I'm trying to get the "HelloWorld" as a string : "java.lang.ClassCastException: org.ksoap2.serialization.SoapPrimitive"

        Object result = (Object)envelope.getResponse();
        String strRes = (String)result; // this throws the exception

Getting 'result' as a string always produces an exception even if I try to call 'result.toString()' methode.

Very strange, because When I debug the code, I can see that the 'result' Object has got Its 'value' property set to the good value...

Any idea ? Thanks, Vince.

Vince
A: 

@Vince Typecast the envelope to SoapPrimitive:

SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

String strRes = result.toString();

and it;ll worlk

ali
+1  A: 

Hey its very simple the thing is you are getting the result into an Object which is a primitive one

Your Code:

Object result = (Object)envelope.getResponse();

Correct Code:

SoapObject result=(SoapObject)envelope.getResponse();

//to get the data
String resultData=result.getProperty(0).toString();
// 0 is the first object of data 

I think this should definetly work.

HaKr