调用soap wsdl web服务android

时间:2016-06-11 10:37:48

标签: android web-services soap-client

我使用java创建Web服务,我有链接

http://localhost:8181/NetBeansProjects/WsTlu30LichPhongVan?WSDL

  

在android studio上我使用EasyWSDL Generator插件通过链接调用WS并生成HFIWsDangNhapPortBinding.java,然后我有代码:

public String testLogin(String username, String pass) {
    String result = "";
    try {
        result =  wsDangNhapPortBinding.TestLogin(username, pass);
    } catch (Exception e) {
        result = "catch";
        e.getStackTrace();
    }
    return result;
}

当我打电话给testLogin时才抓到?

1 个答案:

答案 0 :(得分:1)

使用以下代码从Android应用程序调用soap web服务。它需要ksoap库,所以下载并添加ksoap库到你的android项目

public class WebServices {
    private static String serviceResponse;
    final static String NAMESPACE = "http://tempuri.org/";
    final static String URL = "http://" + AppConstants.IP + "/MobileService.asmx ";


    public static String dynamicWebCall(String methodName,HashMap<String,String> parameters) {

        String SOAP_ACTION = "http://tempuri.org/"+methodName;
        try {
            SoapObject request = new SoapObject(NAMESPACE, methodName);
            if(parameters != null){
                for (Map.Entry<String, String> para : parameters.entrySet()) {
                    request.addProperty(para.getKey(), para.getValue());
                }
            }
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            new MarshalBase64().register(envelope); // serialization
            envelope.dotNet = true;
            Log.d("test", "URL = " + URL);
            Log.d("test", "request= " + request.toString());
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            if (response != null) {
                serviceResponse = response.toString();
            } else {
                serviceResponse = "0";
            }

            Log.d("test", methodName + "response = " + serviceResponse);
        } catch (Exception e) {
            Log.d("test", "Error - " + e.toString());
            serviceResponse = "Error";
        }
        return serviceResponse;
    }
}