I have been struggling the entire day to connect an Android app to a simple WCF Service (It is a proof of concept app). I have looked everywhere for help, but I can still cannot come right.
I want to return a string when a GetHello() service contract method is called.
The service is as follows:
using System.ServiceModel;
namespace MyWcfServiceLibrary
{
[ServiceContract]
&// ReSharper disable InconsistentNaming
public interface IInventoryDTOService
$// ReSharper restore InconsistentNaming
{
[OperationContract]
string GetHello();
[OperationContract]
int GetNumber(bool getPositiveNumber);
[OperationContract]
InventoryDTO GetInventoryDTO(int id);
// TODO: Add your service operations here
}
}
The service impelementation is a follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace MyWcfServiceLibrary
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
public class InventoryDTOService : IInventoryDTOService
{
public string GetHello()
{
return "Hello From WCF";
}
public int GetNumber(bool getPositiveNumber)
{
int returnInt = -55;
if (getPositiveNumber == true)
{
returnInt = 55;
}
return returnInt;
}
public InventoryDTO GetInventoryDTO(int id)
{
InventoryDTO dto = new InventoryDTO()
{
Donor = "Harold Potter",
InventoryId = 22,
PerishDate = DateTime.Now
};
return dto;
}
}
}
The Android java is as follows:
package FoodVault.Mobile.DemoWCFClient;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.*;
import org.xmlpull.v1.XmlPullParserException;
import org.ksoap2.transport.HttpTransportSE;
public class DemoWCFClientActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final String METHOD_NAME = "GetHello";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://41.3.235.46/Service/InventoryDTO.svc";
private static final String SOAP_ACTION = "http://tempuri.org/IInventoryDTOService/GetHello";
EditText editText;
Button buttonUpdate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) findViewById(R.id.editText);
buttonUpdate=(Button)findViewById(R.id.buttonUpdate);
buttonUpdate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// request.addProperty("name", "Qing");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = null;
response = (SoapObject) envelope.getResponse();
String resultData= response.getProperty(0).toString();
editText.setText(resultData);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The WSDL:
wsdl:definitions name="InventoryDTOService" targetNamespace="http://tempuri.org/">
−
<wsdl:types>
−
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://41.4.90.184/Service/InventoryDTO.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://41.4.90.184/Service/InventoryDTO.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://41.4.90.184/Service/InventoryDTO.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/MyWcfServiceLibrary"/>
</xsd:schema>
</wsdl:types>
−
<wsdl:message name="IInventoryDTOService_GetHello_InputMessage">
<wsdl:part name="parameters" element="tns:GetHello"/>
</wsdl:message>
−
<wsdl:message name="IInventoryDTOService_GetHello_OutputMessage">
<wsdl:part name="parameters" element="tns:GetHelloResponse"/>
</wsdl:message>
−
<wsdl:message name="IInventoryDTOService_GetNumber_InputMessage">
<wsdl:part name="parameters" element="tns:GetNumber"/>
</wsdl:message>
−
<wsdl:message name="IInventoryDTOService_GetNumber_OutputMessage">
<wsdl:part name="parameters" element="tns:GetNumberResponse"/>
</wsdl:message>
−
<wsdl:message name="IInventoryDTOService_GetInventoryDTO_InputMessage">
<wsdl:part name="parameters" element="tns:GetInventoryDTO"/>
</wsdl:message>
−
<wsdl:message name="IInventoryDTOService_GetInventoryDTO_OutputMessage">
<wsdl:part name="parameters" element="tns:GetInventoryDTOResponse"/>
</wsdl:message>
−
<wsdl:portType name="IInventoryDTOService">
−
<wsdl:operation name="GetHello">
<wsdl:input wsaw:Action="http://tempuri.org/IInventoryDTOService/GetHello" message="tns:IInventoryDTOService_GetHello_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IInventoryDTOService/GetHelloResponse" message="tns:IInventoryDTOService_GetHello_OutputMessage"/>
</wsdl:operation>
−
<wsdl:operation name="GetNumber">
<wsdl:input wsaw:Action="http://tempuri.org/IInventoryDTOService/GetNumber" message="tns:IInventoryDTOService_GetNumber_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IInventoryDTOService/GetNumberResponse" message="tns:IInventoryDTOService_GetNumber_OutputMessage"/>
</wsdl:operation>
−
<wsdl:operation name="GetInventoryDTO">
<wsdl:input wsaw:Action="http://tempuri.org/IInventoryDTOService/GetInventoryDTO" message="tns:IInventoryDTOService_GetInventoryDTO_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IInventoryDTOService/GetInventoryDTOResponse" message="tns:IInventoryDTOService_GetInventoryDTO_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
−
<wsdl:binding name="BasicHttpBinding_IInventoryDTOService" type="tns:IInventoryDTOService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
−
<wsdl:operation name="GetHello">
<soap:operation soapAction="http://tempuri.org/IInventoryDTOService/GetHello" style="document"/>
−
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
−
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
−
<wsdl:operation name="GetNumber">
<soap:operation soapAction="http://tempuri.org/IInventoryDTOService/GetNumber" style="document"/>
−
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
−
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
−
<wsdl:operation name="GetInventoryDTO">
<soap:operation soapAction="http://tempuri.org/IInventoryDTOService/GetInventoryDTO" style="document"/>
−
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
−
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
−
<wsdl:service name="InventoryDTOService">
−
<wsdl:port name="BasicHttpBinding_IInventoryDTOService" binding="tns:BasicHttpBinding_IInventoryDTOService">
<soap:address location="http://41.4.90.184/Service/InventoryDTO.svc/Design_Time_Addresses/MyWcfServiceLibrary/InventoryDTOService/""/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
When I call the androidTransport.call(...) method I keep getting a socket timeout exception. Any advice will be greatly appreciated.
You probably have access issues to the IP number. Make sure it is reachable e.g. via the browser on the device or emulator. The ksoap2-android website also has more links that should help with that.