2016-06-11 181 views
1

我使用Java創建Web服務,我有鏈接呼叫肥皂WSDL web服務的Android

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

在Android工作室我使用EasyWSDL發電機插件通過鏈接調用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

使用以下代碼從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; 
    } 
}