0

Using Android ksoap2, I'm trying to communicate with an example .NET web service. I keep getting http response code 415. I've tried many things, many from this site and would be grateful for any ideas..

Service.cs:

public class Service : IService
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

IService.cs:

[ServiceContract]
public interface IService
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

Android:

try
{                 
    SoapPrimitive response;
    SoapObject request = new SoapObject("http://tempuri.org/", "GetData"); 

    request.addProperty("value", 0); 

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

    HttpTransportSE androidHttpTransport = new HttpTransportSE("http://www.testsite.com/servicetest/Service.svc"); 

    androidHttpTransport.call("http://tempuri.org/GetData", envelope); 

    response=(SoapPrimitive)envelope.getResponse(); 
}
catch(Exception e)
{
    Log.e("Exception:",e.getMessage());
}

The exception always occurs on the androidHttpTransport.call line with 'HTTP request failed, HTTP status:415. Debugging the web service, I can see it works on localhost and http://www.testsite.com/servicetest/, but not when called from the Android app. Any ideas?

Oliver Hanappi
  • 12,046
  • 7
  • 51
  • 68
Gwentech
  • 13
  • 3
  • can you able to get http://www.testsite.com/servicetest/ in android browser – Sree Jul 17 '14 at 11:56
  • Yes, though I don't know how to call the functions from the android browser, I see the listing of routines in testsite.com/servicetest/ – Gwentech Jul 17 '14 at 12:58

1 Answers1

0

After what seems like forever, I found something that works. In the Web service interface, put this in with ServiceContract and OperationContract. Also use XmlSerializer:

[ServiceContract (Namespace="http://tempuri.org")]
public interface IService
{

    [OperationContract(Action = "http://tempuri.org/GetData")]
    [XmlSerializerFormat(SupportFaults = false)]
    string GetData(int value);

In the Android code, I was using SoapEnvelope.VER12 and should have been using SoapEnvelope.VER11.

Gwentech
  • 13
  • 3