简体   繁体   English

Android-将数据发送到Winforms中托管的WCF服务

[英]Android - Send data to WCF Service hosted in Winforms

I have been through the normal channels to try and solve this issue, but I can't seem to nail down how to send data correctly from my Android app to a WCF library hosted in my C# winforms app. 我已经通过常规渠道尝试解决此问题,但是我似乎无法确定如何正确地将数据从Android应用程序正确发送到C#winforms应用程序中托管的WCF库。

I have checked that the service is hosted properly using the WCFClientTest app provided by visual studio and everything is working on that end. 我已经检查了该服务是否使用Visual Studio提供的WCFClientTest应用程序正确托管,并且一切都在正常进行。 When I call this line: androidHttpTransport.call(SOAP_ACTION, envelope); 当我调用此行时: androidHttpTransport.call(SOAP_ACTION,信封); it appears to hang in the Android app 它似乎挂在Android应用程序中

I am using ksoap2 in an attempt to call into the WCF Service, with this link as a tutorial. 我正在使用kso​​ap2尝试调用WCF服务,并将此链接作为教程。

Android Code Android代码

private static final String METHOD_NAME = "Alive";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String SOAP_ACTION 
                               = "http://tempuri.org/IDeviceHealth/Alive";

    //10.0.2.2 is the ip address for the device simulator.
    private static final String URL = "http://192.168.1.8:8733/DeviceHealth/HealthService/mex"; 

    //SOAP must be  the same version as the webservice.
    private static final int SOAP_VERSION = SoapEnvelope.VER12;

public void OnClickGoBtn(View v)
    {
        try 
        {
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.call(SOAP_ACTION, envelope);
        Object result = envelope.getResponse();

        String resultData = result.toString();
        Log.d("WCF Response", resultData);          
        } 
        catch (IOException e) 
        {
          Log.d("WCF IO Error", e.toString());
        }
        catch (XmlPullParserException e) 
        {
            Log.d("XML Pull Error",e.toString());
        }
        catch(Exception e)
        {
            Log.d("General Exception", e.toString());
        }
    }

C# Server: C#服务器:

 <behaviors>
      <serviceBehaviors>
        <behavior name="MetadataBehavior">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service name="DeviceHealthService.DeviceHealth" behaviorConfiguration="MetadataBehavior">

        <endpoint address=""
                  binding="netTcpBinding"
                  contract="DeviceHealthService.IDeviceHealth"/>

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"/>

        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:10001"/>
            <add baseAddress="http://localhost:10002"/>
          </baseAddresses>
        </host>

      </service>
    </services>

public string Alive()
        {
            return "I am running";
        }

You should be doing this inside an AsyncTask, and not on the main thread! 您应该在AsyncTask内部而不是在主线程上执行此操作! From your post, it looks like you're doing this from the main thread. 从您的帖子看来,您似乎正在从主线程执行此操作。

Everything else about your code looks normal. 关于代码的所有其他内容看起来都很正常。 I typically use a timeout though, and I see you don't. 不过,我通常会使用超时,但我看不到。

Another thing to note is that you are pointing your URL at "localhost", but your android device doesn't know what localhost is. 要注意的另一件事是,您将URL指向“ localhost”,但是您的android设备不知道什么是localhost。 You should point at 10.0.2.2 instead. 您应该改为指向10.0.2.2。

If it helps, here's a utility method I use in one of my Android applications that consumes a SOAP based WCF service. 如果有帮助,这是我在一个使用基于SOAP的WCF服务的Android应用程序中使用的实用程序方法。 Please note that I always call this from within an AsyncTask, never on the main thread. 请注意,我总是从AsyncTask内部调用此函数,而不是在主线程上调用。

public static String invokeSoapWebservice(String method, Bundle properties, int timeout) throws IOException, XmlPullParserException {

    String action = SOAP_ACTION + method;       

    System.out.println("Calling web method " + method);

    SoapObject request = new SoapObject(NAMESPACE, method);

    //Note - ORDER MATTERS HERE
    //If the web method takes employeeId, workSiteId, jobId in that order, 
    //they have to be added to the request in that order (what bullshit!)

    if(properties.containsKey("loginId")) {
        request.addProperty("loginId", properties.getInt("loginId"));
        System.out.println("loginId = " + properties.getInt("loginId"));
    }

    if(properties.containsKey("employeeId")) {
        request.addProperty("employeeId", properties.getInt("employeeId"));
        System.out.println("employeeId = " + properties.getInt("employeeId"));
    }

    if(properties.containsKey("workSiteId")) {
        request.addProperty("workSiteId", properties.getInt("workSiteId"));
        System.out.println("workSiteId = " + properties.getInt("workSiteId"));
    }

    if(properties.containsKey("jobId")) {
        request.addProperty("jobId", properties.getInt("jobId"));
        System.out.println("jobId = " + properties.getInt("jobId"));
    }

    if(properties.containsKey("laborDetailId")) {
        request.addProperty("laborDetailId", properties.getInt("laborDetailId"));
        System.out.println("laborDetailId = " + properties.getInt("laborDetailId"));
    }

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

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL, timeout);       

    androidHttpTransport.call(action, envelope);
    Object result = envelope.getResponse();
    String resultData = "null response";
    if(result != null) {
        resultData = result.toString();
    }

    System.out.println("Result for web method " + method + ": " + resultData);

    return resultData;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM