2011-07-17 46 views
0

我一直在努力將整個Android應用程序連接到一個簡單的WCF服務(這是一個概念應用程序的證明)。 我已經到處尋找幫助,但我仍然不能來。在Android中使用WCF服務

我想在調用GetHello()服務契約方法時返回一個字符串。

服務如下:

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

     } 
    } 

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(...)方法我不斷收到一個套接字超時異常。任何意見將不勝感激。

回答

2

您可能有訪問問題的IP號碼。確保它可以到達,例如通過設備或模擬器上的瀏覽器。 ksoap2-android網站也有更多的鏈接,應該有所幫助。

+0

我已經解決了套接字超時異常,我需要使用IP地址:10.0.2.2使用這個資源:http://developer.android.com/resources/faq/commontasks的.html#localhostalias。現在我不斷收到一個XMLPullParserException:org.xmlpull.v1.XmlPullParserException:意外類型(位置:END_DOCUMENT null @ 1:0,在[email protected]) – Lee

+0

如果我輸入服務的URL:http:// 10.0.2.2:80/Service/InventoryDTO.svc在模擬器瀏覽器中現在可見。 – Lee

+0

那麼你應該接受我的答案。拉解析器是一個不同的問題。請閱讀wiki,瞭解如何調試您的請求和響應xml .. –

0

我不熟悉Android平臺,但我曾經研究過Java客戶端連接到WCF服務,它使用Glassfish很好(也許我不記得它)。

我可能會給你一些建議。您可以打開WCF跟蹤並查看服務器端出了什麼問題,日誌可能會顯示一些信息。

+0

謝謝,我會試試這個 – Lee

0

在我的項目中,當我在GPRS [慢速網絡]上嘗試時出現SoketTimeoutException,並且這對於wifi工作正常。

也關閉您的防火牆,並禁用您的防病毒如果你已經安裝,那麼它會工作。