在Android上使用WCF服务

时间:2011-07-17 23:17:51

标签: android wcf ksoap2

我一直在努力将Android应用程序连接到简单的WCF服务(这是一个概念验证应用程序)。 我到处寻找帮助,但我仍然无法正确行事。

我希望在调用GetHello()服务契约方法时返回一个字符串。

服务如下:

使用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;
            }

        }
    }

Android java如下:

包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();
                }

        }

        }

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>

当我调用androidTransport.call(...)方法时,我不断收到套接字超时异常。任何建议将不胜感激。

3 个答案:

答案 0 :(得分:2)

您可能对IP号码有访问权限。确保它可以到达,例如通过设备或模拟器上的浏览器。 ksoap2-android网站还有更多链接可以帮助解决这个问题。

答案 1 :(得分:0)

我不熟悉Android平台,但我曾经知道Java客户端连接到WCF服务,它使用Glassfish很好用(也许,我不记得了)。

我可能会给你一些建议。您可以打开WCF跟踪并查看服务器端出现的问题,日志可能会显示一些内容。

答案 2 :(得分:0)

在我的项目中,当我在GPRS [慢速网络]上尝试它时,我得到了SoketTimeoutException,这对于wifi工作正常。

如果已经安装了防火墙,请关闭防火墙并禁用防病毒软件,然后才能运行。