I am learning how to create an android app and webservice using: Eclipse IDE ksoap2 version 2.6.5 with dependencies Visual studio 2010
my method was to follow this great tutorial: http://www.youtube.com/watch?v=v9EowBVgwSo&feature=related
the tutorial works fine with the w3schools test webservice.
I then attempted to write my own webservice and used the helloworld webservice provided in Visual studio to start me off. I ammended my code to point at the new webservice (published on my development laptop) and ran it on my handset in debug mode.
the communication with the webservice is fine when calling the HelloWorld() webservice. it even returns a result from simpleMethod() but it does not include the parameter that I passed it. e.g. when calling simpleMethod, Iam returned the following string: "Hello "
I think this is an issue with the webservice because it works for the w3schools test webservice but I am stuck and would appreciate some help.
my code follows:
Webservice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WMCMobileWebService
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://testuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string simpleMethod(String srt)
{
return "Hello " + srt;
}
}
}
Android app MainActivity:
package com.clcltd.soaptest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class SoapTestActivity extends Activity {
private static final String SOAP_ACTION = "http://testuri.org/simpleMethod";
private static final String METHOD_NAME = "simpleMethod";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.168.2.22/wmcmobilewebservice/Service1.asmx";
PropertyInfo pi;
TextView tv, tvc;
String str;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tvc = (TextView) findViewById(R.id.textView2);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
str = "Andy";
pi = new PropertyInfo();
pi.setName("srt");
pi.setValue(str);
pi.type = PropertyInfo.STRING_CLASS;
request.addProperty(pi);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(request);
HttpTransportSE aht = new HttpTransportSE(URL);
try{
aht.call(SOAP_ACTION, soapEnvelope);
SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
tv.setText("Status: " + resultString);
tvc.setText(request.toString());
}catch (Exception e)
{
e.printStackTrace();
tv.setText("Error" + e.toString());
}
}
}
When running the code above, I get the following output on my screen:
Status: Hello simpleMethod{srt=Andy; }
I would expect the result to be:
Status: Hello Andy
Any ideas?
I spent a while on this, but in the end I found another tutorial (http://sarangasl.blogspot.co.uk/2010/09/create-simple-web-service-in-visual.html) detailing how to use the KSOAP2 webservice on an older version of KSOAP.
The tutorial includes a link to the code which contains an older version of KSOAP.
As a test I put the latest KSOAP version in this new Project and it stopped working, so it looks like my issue was version related after all.
Hopefully pointing others at this tutorial will help them out.