在android中使用WCF服务

在android中使用WCF服务,android,wcf,Android,Wcf,我已创建此服务(使用WCF、Azure): 其中IPMPService是: [ServiceContract] public interface IPMPService { [WebGet()] [OperationContract] int Dummy(); } 并试图在我的android应用程序中使用它: 第一次尝试: String METHOD_NAME = "DummyRequest"; String NAMESPACE = "ht

我已创建此服务(使用WCF、Azure):

其中IPMPService是:

[ServiceContract]
public interface IPMPService
{
    [WebGet()]
    [OperationContract]
    int Dummy();
}
并试图在我的android应用程序中使用它:
第一次尝试:

String METHOD_NAME = "DummyRequest";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://tyty.cloudapp.net/Service1.svc";
String SOAP_ACTION = "http://tyty.cloudapp.net/Service1.svc/Dummy";

String res = "";
try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    // to get the data
    String resultData = result.toString();
    res = resultData;
    // 0 is the first object of data
    } catch (Exception e) {
        res = e.getMessage();
    }   
结果:SoapFault-faultcode:'a:ActionNotSupported'faultstring:'The message with Action'http://tyty.cloudapp.net/Service1.svc/Dummy由于EndpointDispatcher上的ContractFilter不匹配,无法在接收器上处理。这可能是因为合同不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。'faultactor:'null'详细信息:null

第二次尝试(摘自):

结果:java.net.UnknownHostException:无法解析主机“http://tyty.cloudapp.net“:没有与主机名关联的地址

我还在c#中创建了一个(工作)客户端,下面是它的App.config:

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IPMPService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://tyty.cloudapp.net/Service1.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPMPService"
          contract="IPMPService" name="BasicHttpBinding_IPMPService" />
    </client>
  </system.serviceModel>
</configuration>

如前所述,Android中的两次尝试都失败了。有人能告诉我我做错了什么,以及如何纠正吗?

谢谢。

您的第一次尝试非常接近。只需要两个修复程序:

String METHOD_NAME = "Dummy";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://tyty.cloudapp.net/Service1.svc";
String SOAP_ACTION = "http://tempuri.org/IPMPService/Dummy";

String res = "";
try {
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
    // to get the data
    String resultData = result.toString();
    res = resultData;
    // 0 is the first object of data
} catch (Exception e) {
    res = e.getMessage();
}   
第一个是方法名:它只是
Dummy
(而不是
DummyRequest
)。第二个是SOAP操作:它是
http://tempuri.org/IPMPService/Dummy

两者都可以从WSDL派生。您的操作定义为:

<wsdl:operation name="Dummy">
    <wsdl:input wsaw:Action="http://tempuri.org/IPMPService/Dummy" message="tns:IPMPService_Dummy_InputMessage"/>
    <wsdl:output wsaw:Action="http://tempuri.org/IPMPService/DummyResponse" message="tns:IPMPService_Dummy_OutputMessage"/>
</wsdl:operation>
在这里可以找到SOAP操作:

<wsdl:operation name="Dummy">
    <soap:operation soapAction="http://tempuri.org/IPMPService/Dummy" style="document"/>
    ...

...
公共类RESTConnection扩展了全局
{
Global objglobal=Global.getInstance();
私有静态字符串服务\u URI=“您的服务路径”;
公共JSONObject GetCashierLogin(字符串用户名、字符串密码)
{
新的ArrayList();
JSONObject jObj=null;
尝试
{
SERVICE_URI=objglobal.getConnectionString();
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpGet请求=新的HttpGet(服务URI+”/LoginByUserAndPassword?Username=“+Username+”&password=“+password+”);
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
HttpResponse response=httpClient.execute(请求);
HttpEntity responseEntity=response.getEntity();
String bufferLogin=EntityUtils.toString(responseEntity,HTTP.UTF_8);
JSONArray JSONArray=新的JSONArray(bufferLogin);
if(jsonArray!=null)
{
for(int i=0;i
我只能在18小时内给你奖金。但愿上帝保佑你。
<wsdl:operation name="Dummy">
    <wsdl:input wsaw:Action="http://tempuri.org/IPMPService/Dummy" message="tns:IPMPService_Dummy_InputMessage"/>
    <wsdl:output wsaw:Action="http://tempuri.org/IPMPService/DummyResponse" message="tns:IPMPService_Dummy_OutputMessage"/>
</wsdl:operation>
<wsdl:message name="IPMPService_Dummy_InputMessage">
    <wsdl:part name="parameters" element="tns:Dummy"/>
</wsdl:message>
<wsdl:operation name="Dummy">
    <soap:operation soapAction="http://tempuri.org/IPMPService/Dummy" style="document"/>
    ...
public class RESTConnection extends Global 
{


    Global objglobal=Global.getInstance();

    private static String SERVICE_URI = "your service path";

    public JSONObject GetCashierLogin(String UserName, String Password)
    {
        new ArrayList<String>();  

        JSONObject jObj = null;  
        try 
        {

            SERVICE_URI=objglobal.getConnectionString();
            DefaultHttpClient httpClient = new DefaultHttpClient(); 
            HttpGet request = new HttpGet(SERVICE_URI+ "/LoginByUserIdAndPassword?Username=" + UserName + "&password=" + Password + "");
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-type", "application/json");
            HttpResponse response = httpClient.execute(request);
            HttpEntity responseEntity = response.getEntity();
            String bufferLogin = EntityUtils.toString(responseEntity, HTTP.UTF_8);
            JSONArray jsonArray = new JSONArray(bufferLogin);

            if (jsonArray != null)
            {
                for (int i = 0; i < jsonArray.length(); i++)
                {
                    jObj=(JSONObject) jsonArray.get(i);

                }
            }

        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return jObj;
    }