WCF自托管服务KSOAP(android)

时间:2013-12-07 13:06:04

标签: c# android wcf web-services ksoap

我已经拥有了我的Android应用程序。到目前为止,我一直在使用KSOAP与Web服务通信 - 标准的asmx文件。一切正常,但一个月以来我一直在尝试使用自托管的WCF服务为我的应用程序提供数据。

我已经完成了WCF配置的所有组合,但仍然出现了问题。

请大家,我需要像KSOAP的HelloWorld WCF服务一样简单的示例项目。配置端点,空间等。 我将非常感激。

我花了很多时间在网上搜索,但我无法根据我找到的信息建立工作服务。

这是我第一次寻求帮助...... 最诚挚的问候来自波兰

1 个答案:

答案 0 :(得分:0)

我有同样的问题,并以这种方式解决(WCF绑定必须是basicHttpBinding,否则它不起作用):

private static final String NAMESPACE = "http://tempuri.org/";
private static String URL="your url";

private static final String SOAP_ACTION_VALIDATION = "IValidateUser_wcf/ValidateUser";
private static final String VALIDATION_METHOD = "ValidateUser";

public boolean validateUser_WCF(String username, String password){

    SoapSerializationEnvelope envelope = null;
    SoapObject request = null;
    HttpTransportSE httpTransportSE = null;

    try {
        request = new SoapObject(NAMESPACE, VALIDATION_METHOD);
        request.addProperty("username", username);
        request.addProperty("password", password);

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

        //////////////////////////////                               
        // here you can add a HEADER element if you want
        Element[] header = new Element[1];  

        header[0] = new Element().createElement(NAMESPACE_INFOCAD, "a1");                
        header[0].addChild(Node.TEXT, "HeaderTextContent");

        envelope.headerOut = header;
        //////////////////////////////                               

        // second parameter is timeout:
        httpTransportSE = 
            new HttpTransportSE(URL+VALIDATION_URI, 10*10000); 
        httpTransportSE.debug = true;
        httpTransportSE.setXmlVersionTag(
           "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        httpTransportSE.call(NAMESPACE+SOAP_ACTION_VALIDATION, envelope);

        // if response is a simple text result, you can call
        // SoapPrimitive, if not, you have to call SoapObject 
        // result and navigate in response's tree like an xml file
        SoapPrimitive result = (SoapPrimitive)envelope.getResponse();

        //To get the data.
        String textResult = result.toString();
        Log.i("textResult", textResult); 

        return true;

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }finally{
       // here you can see in LOG what is request you send and what is response received
        Log.i(getClass().getSimpleName(),"requestDump : "+httpTransportSE.requestDump);
        Log.i(getClass().getSimpleName(),"responseDump : "+httpTransportSE.responseDump);
    }

    return false;
}