将Java / Android连接到.NET Web服务

时间:2013-08-17 18:28:28

标签: c# java android .net web-services

我刚用HelloWorld方法构建一个.net默认的webservice!

现在我正在尝试连接Java / Android简单实现以连接.net webservice。我被困在HttpTransportSE电话上。它不起作用,实际上它停在这里。没有错误。

这里是代码示例:

private class SoapAccessTask extends AsyncTask<String, Void, String> {

  protected String doInBackground(String... urls){
     final String METHOD_NAME = "HelloWorld";
     final String NAMESPACE = "http://tempuri.org";
     final String SOAP_ACTION = "http://tempuri.org/HelloWorld;
     final String URL = "http://localhost:2986/Service1.asmx;

     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
     SoapSerializationEnvelop envelop = new SoapSerializationEnvelop(SoapEnvelop.VER11);
     AndroidHttpTransport androidHttp = new AndroidHttpTransport(URL);
     androidHttp.call(SOAP_ACTION, envelop);
  }
}

知道为什么这不起作用吗?

此致

2 个答案:

答案 0 :(得分:1)

在代码中添加以下行:

envelope.dotNet = true;
envelope.setOutputSoapObject(request);

修改

  1. 对于来自kSOAP2的.NET Web服务调用,dotNet标志必须为true(因为您使用的是.NET Web服务)。因此envelope.dotNet = true;

  2. 最后,SoapObject实例“request”被指定为对信封的soap调用的出站消息。因此envelope.setOutputSoapObject(request);

答案 1 :(得分:0)

如果您使用kso​​ap2访问asmx web方法,此代码可能对您有所帮助

public class ServiceHandler {
    private static String SOAP_ACTION;    
    private static String OPERATION_NAME;    
    private static final String WSDL_TARGET_NAMESPACE = "http://xxx.example.com/";    
    private static final String SOAP_ADDRESS = "http://xxx.example.com/WebService.asmx";

    /**
     * call WebMethod
     * 
     * @param param1 
     * @param param2 
     * 
     * @return //sample code
     */
    public static String callWebMethod(String param1,String param2){

        OPERATION_NAME = "myFunc";
        SOAP_ACTION = "http://xxx.example.com/myFunc";

        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

        request.addProperty("param1",param1);
        request.addProperty("param2",param2);

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

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

        try{                  
            httpTransport.call(SOAP_ACTION, envelope);      
            SoapObject object = (SoapObject) envelope.getResponse();
            //get object
        }catch(Exception exception){
            System.out.println(exception.toString());
            return "";          
        }
    }

    return "";
}