How to call .NET Webservice with Android using KSOAP2?

I have a problem calling webservice. I have a .NET web service on the server and I am using KSOAP2 (ksoap2-j2se-full-2.1.2) on Android. During program startup, I received a run-time exception, such as "org.ksoap2.serialization.SoapPrimitive". What should I 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()); } } } 
+47
android ksoap2 ksoap
Jun 27 '09 at 7:23
source share
6 answers

It is very simple. You get the result in Object , which is primitive.

Your code:

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

The correct code is:

 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 definitely work.

+24
Jan 12
source share

What does your .NET Webservice look like?

I had the same effect using ksoap 2.3 with code.google.com . I followed the tutorial on Draft Code (this is a great BTW.)

And every time I used

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

to get the result of my web service (regardless of type, I tried Object, String, int) I came across an org.ksoap2.serialization.SoapPrimitive exception.

I found a solution (workaround). The first thing I needed to do was 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 that has a “value” that is private. Fortunately, the value is passed using the toString() method, so I use Integer.parseInt(result.toString()) to get my value, which is enough for me, because I don't have any complex types that I need to get from my web service.

Here is the complete 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()); } 
+12
Oct 05 '09 at 17:15
source share

Enter the option "SoapPrimitive":

 SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); String strRes = result.toString(); 

and it will work.

+7
Dec 17 '09 at 18:22
source share

You can use the code below to call the web service and get a response. Make sure your web service returns the response in the Data Table Format . This code will help you if you are using data from SQL Server . If you are using MYSQL , you need to change one thing by simply replacing the word NewDataSet with the sentence obj2=(SoapObject) obj1.getProperty("NewDataSet"); on DocumentElement strong>

 private static final String NAMESPACE = "http://tempuri.org/"; private static final String URL = "http://localhost/Web_Service.asmx?"; // you can use IP address instead of localhost private static final String METHOD_NAME = "Function_Name"; private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME; SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("parm_name", prm_value); // Parameter for Method SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try { androidHttpTransport.call(SOAP_ACTION, envelope); //call the eb service Method } catch (Exception e) { e.printStackTrace(); } //Next task is to get Response and format that response SoapObject obj, obj1, obj2, obj3; obj = (SoapObject) envelope.getResponse(); obj1 = (SoapObject) obj.getProperty("diffgram"); obj2 = (SoapObject) obj1.getProperty("NewDataSet"); for (int i = 0; i < obj2.getPropertyCount(); i++) //the method getPropertyCount() return the number of rows { obj3 = (SoapObject) obj2.getProperty(i); obj3.getProperty(0).toString(); //value of column 1 obj3.getProperty(1).toString(); //value of column 2 //like that you will get value from each column } 

If you have problems with this, you can write to me.

+4
May 11 '12 at 12:14
source share

If more than one result is expected, the getResponse() method returns a Vector containing various answers.

In this case, the violation code will look like this:

 Object result = envelope.getResponse(); // treat result as a vector String resultText = null; if (result instanceof Vector) { SoapPrimitive element0 = (SoapPrimitive)((Vector) result).elementAt(0); resultText = element0.toString(); } tv.setText(resultText); 

The answer is based on ksoap2-android (mosabua widget)

0
Sep 27 '11 at 12:45
source share

I think you cannot call

  androidHttpTransport.call(SOAP_ACTION, envelope); 

on the main topic.

Network operations must be performed for different threads.

Create another Thread or AsyncTask to call the method.

0
Aug 26 '13 at 15:36
source share



All Articles