I have created a web service using ASP.NET then I'm having a problem in connecting to it using android app. The method Hello world in my code work correctly because it has no arguments but the method add doesn't.
//ASP.NET code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://ahmadezzat.com/")]
[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 WebService : System.Web.Services.WebService {
SqlConnection connection = new SqlConnection();
public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int HelloWorld()
{
return 5;
}
[WebMethod]
public int echo(int x) {
return x;
}
}
Android code
package com.me;
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;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SignUp extends Activity {
private static final String SOAP_ACTION = "http://ahmadezzat.com/echo";
private static final String METHOD_NAME = "echo";
private static final String NAMESPACE = "ServiceReference1";
private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
Button button = (Button) findViewById(R.id.signUp);
final TextView tv = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String result = callWebService(22);
tv.setText("the result is " + result);
}
});
}
public String callWebService(int x) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//request.addProperty("msg", "1");
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(x);
pi.setType(int.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(urll);
androidHttpTransport.debug = true;
//return androidHttpTransport.requestDump + " / " + androidHttpTransport.responseDump;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive res = (SoapPrimitive) envelope.getResponse();
String v = res.toString() + "\n";
v += androidHttpTransport.requestDump + "\n" + androidHttpTransport.responseDump;
return v;
} catch (Exception E) {
return E.toString();
}
}
}
After tracing the request it was like this
<v:Body>
<echo xmlns="ServiceReference1" id="o0" c:root="1">
<x i:type="d:int">22</x>
<echo>
<v:/Body>
and the response is
<Soap:Body>
<echoResponse xmlns="http://www.ahmadezzat.com"/>
<echoResult>0</echoResult>
<echoResponse>
<Soap:/Body>
You just have one problem in the Code: User this namespace "http://ahmadezzat.com/" instead of "ServiceReference1"
private static final String NAMESPACE = "http://ahmadezzat.com/";
If you tried to test the android code over android version 4.* it will case exception "NetworkOnMainThreadException" and don't work well with you. So you need to user AsyncTask to avoid that.
developer.android.com/reference/android/os/AsyncTask.html
Android Code after editing:
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;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SignUp extends Activity {
private static final String SOAP_ACTION = "http://ahmadezzat.com/echo";
private static final String METHOD_NAME = "echo";
private static final String NAMESPACE = "http://ahmadezzat.com/";
private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
Button button = (Button) findViewById(R.id.signUp);
tv = (TextView) findViewById(R.id.textView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new CallWebService().execute(22);
}
});
}
class CallWebService extends AsyncTask<Integer, Void, String> {
@Override
protected String doInBackground(Integer... params) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(params[0]);
pi.setType(int.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(urll);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive res = (SoapPrimitive) envelope.getResponse();
String v = res.toString() + "\n";
v += androidHttpTransport.requestDump + "\n"
+ androidHttpTransport.responseDump;
return v;
} catch (Exception E) {
return E.toString();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv.setText(result);
}
}
}