Daniel Dimitrov
Daniel Dimitrov

Reputation: 59

Android call WCF service

I have a problem with calling WCF web service from Android application. If I call the following URL, "http://10.0.2.2:80/WebService/WebServiceImpl.svc" I get the response "200 OK", but when I try to call the function within the service "http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test" I get a response "400 Bad request".

Can someone help?

namespace WebService
{
    public class WebServiceImpl : IWebServiceImpl
    {
        #region IRestServiceImpl Members
        public string Test()
        {
            return "Test pass";
        }

        #endregion
    }
}

namespace WebService
{
    [ServiceContract]
    public interface IWebServiceImpl
    {
     [OperationContract]
        [WebInvoke(
            Method = "GET")]
        string Test();
    }
}

Android Activity:

    public void onClick(View v)
    {
        // TODO Auto-generated method stub
        HttpClient  client = new DefaultHttpClient();
        String SERVER_HOST="10.0.2.2";
        int SERVER_PORT = 80;
        String URL = "http://10.0.2.2:80/WebService/WebServiceImpl.svc/Test";
        HttpHost target = new HttpHost(SERVER_HOST, SERVER_PORT, "http");
        HttpGet request = new HttpGet(URL);
        try
        {
            HttpResponse response = client.execute(target,request);
            HttpEntity entity = response.getEntity();
            MessageBox(response.getStatusLine().toString());
        }
        catch(Exception e)
        {
            MessageBox("excepton");
            MessageBox(e.toString());
        }
    }

    public void MessageBox(String message){
        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
    }

Upvotes: 3

Views: 4722

Answers (2)

Daniel Dimitrov
Daniel Dimitrov

Reputation: 59

I solve that using "SoapObject"

part of code:

public static final String NAMESPACE = "http://tempuri.org/";
public static final String URL = "http://10.0.2.2:80/MyFirstPublishedWebService/WebServiceImpl.svc?wsdl";  
public static final String SOAP_ACTION = "http://tempuri.org/IWebServiceImpl/Login";
public static final String METHOD_NAME = "Login";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = 
    new SoapSerializationEnvelope(SoapEnvelope.VER11); 

envelope .dotNet = true;

envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
    androidHttpTransport.call(SOAP_ACTION, envelope);
}
catch (Exception e) {

}

Alex, Thanks for answer!

Upvotes: 2

Alex Klimashevsky
Alex Klimashevsky

Reputation: 2495

Firstly test this url in browser. if you have the same problem enable web access for your service in manifest.

Secondary check if ip 10.0.2.2 acceccable from your phone.

Upvotes: 1

Related Questions