首页 文章

如何在Android上调用SOAP Web服务[关闭]

提问于
浏览
521

我很难找到有关如何使用Android调用标准SOAP / WSDL Web服务的良好信息 . 我能找到的所有文件都是非常复杂的文档和对"kSoap2"的引用,然后是一些关于使用SAX手动解析它的一些内容 . 好的,那是's fine, but it' s 2008,所以我认为应该有一些很好的库来调用标准的Web服务 .

Web服务基本上是在NetBeans中创建的 . 我想有IDE支持生成管道类 . 我只需要最简单/最优雅的方式来联系基于Android手机的基于_170456的网络服务 .

26 回答

  • 5

    org.apache.http.impl.client.DefaultHttpClient 默认包含在Android SDK中 . 这将让你连接到WSDL .

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
    HttpResponse response = httpClient.execute(httpGet, localContext);
    
  • 6

    Android不提供任何类型的SOAP库 . 您可以自己编写,也可以使用kSOAP 2之类的东西 . 正如您所说,其他人已经能够在他们自己的项目中编译和使用kSOAP2,但我没有必要 .

    到目前为止,Google已经表明,对于向Android添加SOAP库几乎没有兴趣 . 我对此的怀疑是,他们宁愿支持当前基于REST服务的Web服务趋势,并使用JSON作为数据封装格式 . 或者,使用XMPP进行消息传递 . 但这只是猜想 .

    基于XML的Web服务目前在Android上是一项稍微不重要的任务 . 不了解NetBeans,我不能说那里提供的工具,但我同意应该有更好的库 . XmlPullParser可能会使您无法使用SAX,但我对此并不了解 .

  • 239

    我建议查看一个非常有用的工具,帮助了我很多 . 照顾这个项目的人也很有帮助 . www.wsdl2code.com/

  • 4

    按照SOAP方法执行这些步骤

    从WSDL文件中

    • 为每个请求创建SOAP请求模板 .

    • 然后替换要在代码中传递的值 .

    • 使用DefaultHttpClient实例将此数据发布到服务 endpoints .

    • 最后获取响应流

    • 使用XML Pull解析器解析响应流 .

  • 6

    您可以使用某些 Headers 通过http进行肥皂调用 . 我解决了这个问题没有像ksoap2这样的额外库这里是从肥皂服务获取订单的实时代码

    private static HashMap<String,String> mHeaders = new HashMap<>();
    
    static {
        mHeaders.put("Accept-Encoding","gzip,deflate");
        mHeaders.put("Content-Type", "application/soap+xml");
        mHeaders.put("Host", "35.15.85.55:8080");
        mHeaders.put("Connection", "Keep-Alive");
        mHeaders.put("User-Agent","AndroidApp");
        mHeaders.put("Authorization","Basic Q2xpZW50NTkzMzppMjR3s2U="); // optional
    }public final static InputStream receiveCurrentShipments(String stringUrlShipments)
    {
        int status=0;
        String xmlstring= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ser=\"http://35.15.85.55:8080/ServiceTransfer\">\n" +
                "   <soap:Header/>\n" +
                "   <soap:Body>\n" +
                "      <ser:GetAllOrdersOfShipment>\n" +
                "         <ser:CodeOfBranch></ser:CodeOfBranch>\n" +
                "      </ser:GetAllOrdersOfShipment>\n" +
                "   </soap:Body>\n" +
                "</soap:Envelope>";
        StringBuffer chaine = new StringBuffer("");
    
        HttpURLConnection connection = null;
        try {
            URL url = new URL(stringUrlShipments);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Content-Length", xmlstring.getBytes().length + "");
            connection.setRequestProperty("SOAPAction", "http://35.15.85.55:8080/ServiceTransfer/GetAllOrdersOfShipment");
    
            for(Map.Entry<String, String> entry : mHeaders.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                connection.setRequestProperty(key,value);
    
            }
    
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
    
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(xmlstring.getBytes("UTF-8"));
            outputStream.close();
    
            connection.connect();
            status = connection.getResponseCode();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
    
            Log.i("HTTP Client", "HTTP status code : " + status);
        }
    
        InputStream inputStream = null;
        try {
            inputStream = connection.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return inputStream;
    }
    
  • 1

    调用 ksoap2 方法 . 它工作得非常好 .

    设置细节,比如

    private static String mNAMESPACE=null;
    private static String mURL=null;
    public static Context context=null;
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(Request);
    
    envelope.addMapping(mNAMESPACE, "UserCredentials",new UserCredendtials().getClass());
    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(mURL);
    

    然后得到结果呢

    androidHttpTransport.call(SOAP_ACTION, envelope);
    result = (SoapPrimitive)envelope.getResponse();
    
  • 23

    这是在android中使用SOAP Web服务的一个工作示例 .

    **注意:: **不要忘记在项目中添加ksoap2.jar,并在AndroidManifest文件中添加INTERNET权限

    public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    public final String METHOD_NAME = "FahrenheitToCelsius";
    public final String PROPERTY_NAME = "Fahrenheit";
    public final String SOAP_ACTION = "http://tempuri.org/FahrenheitToCelsius";
    public final String SOAP_ADDRESS = "http://www.w3schools.com/webservices/tempconvert.asmx";
    
    
    private class TestAsynk extends AsyncTask<String, Void, String> {
    
        @Override
        protected void onPostExecute(String result) {
    
            super.onPostExecute(result);
            Toast.makeText(getApplicationContext(),
                    String.format("%.2f", Float.parseFloat(result)),
                    Toast.LENGTH_SHORT).show();
        }
    
        @Override
        protected String doInBackground(String... params) {
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                    METHOD_NAME);
            request.addProperty(PROPERTY_NAME, params[0]);
    
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
    
            envelope.setOutputSoapObject(request);
    
            HttpTransportSE androidHttpTransport = new HttpTransportSE(
                    SOAP_ADDRESS);
            Object response = null;
            try {
    
                androidHttpTransport.call(SOAP_ACTION, envelope);
                response = envelope.getResponse();
                Log.e("Object response", response.toString());
    
            } catch (Exception e) {
                e.printStackTrace();
            }
            return response.toString();
        }
    }
    
  • 20

    我希望Calling a web service from Android有所帮助 .

  • 14

    你可以看看WSClient++

  • 28

    如果可以,请选择JSON . Android附带完整的org.json包

  • 23

    SOAP是一种不适合在Android(或一般移动设备)上使用的技术,因为它需要处理/解析开销 . REST服务是一种更轻量级的解决方案,这就是我的建议 . Android附带一个SAX解析器,使用起来相当简单 . 如果您绝对需要在移动设备上处理/解析SOAP,那么我为您感到遗憾,我能提供的最佳建议就是不使用SOAP .

  • 30

    要从移动设备(特别是在Android手机上)调用Web服务,我使用了一种非常简单的方法来执行此操作 . 我没有使用任何Web服务客户端API来尝试调用Web服务 . 我的方法如下是打电话 .

    • 使用Java标准API HttpURLConnection 创建简单的HTTP连接 .

    • 形成SOAP请求 . (您可以帮助SOAPUI发出SOAP请求 . )

    • 将doOutPut标志设置为true .

    • 设置HTTP标头值,如content-length,Content type和User-agent . 不要忘记设置Content-length值,因为它是必需的 .

    • 将整个SOAP请求写入输出流 .

    • 调用方法 Build 连接并接收响应(在我的情况下,我使用 getResonseCode ) .

    • 如果您收到的响应代码为

    • 这意味着您已成功调用Web服务 .

    • 现在在同一HTTP连接上获取输入流并接收字符串对象 . 此字符串对象是SOAP响应 .

    • 如果响应代码不是200,则在同一HTTP对象上获取 ErrorInput 流并接收错误(如果有) .

    • 使用SAXParser(在我的情况下)或DOMParaser或任何其他解析机制解析收到的响应 .

    我已经为Android手机实现了这个程序,并且它已成功运行 . 即使响应超过700 KB,我也能够解析响应 .

  • 13

    要从android调用SOAP Web服务,请尝试使用此客户端

    不要忘记在你的java构建路径中添加ksoap2-android.jar

    public class WsClient {
        private static final String SOAP_ACTION = "somme";
        private static final String OPERATION_NAME = "somme";
        private static final String WSDL_TARGET_NAMESPACE = "http://example.ws";
        private static final String SOAP_ADDRESS = "http://192.168.1.2:8080/axis2/services/Calculatrice?wsdl";
    
        public String caclculerSomme() {
    
            String res = null;
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                    OPERATION_NAME);
            request.addProperty("a", "5");
            request.addProperty("b", "2");
    
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    
            try {
                httpTransport.call(SOAP_ACTION, envelope);
                String result = envelope.getResponse().toString();
                res = result;
                System.out.println("############# resull is :" + result);
            } catch (Exception exception) {
                System.out.println("########### ERRER" + exception.getMessage());
            }
    
            return res;
        }
    }
    
  • 97

    几个月前我在j2ee应用程序中使用jax-ws web服务,我们使用CXF wsdl2java来生成WS客户端来自WSDL文件的存根以及那些客户端存根我们使用了Web服务 . 几周前,当我试图在Android平台上以相同的方式使用网络服务时,我无法找到任何这样的工具(如果我没有有效地谷歌),以满足我的要求 -

    • 从WSDL获取客户端存根 .

    • 并使用一些参数(java业务请求对象)调用服务 .

    • 获取响应业务对象 .

    所以,我开发了自己的Android SOAP Client Generation Tool . 你必须遵循以下步骤:

    • 来自WSDL获取WS Client Stub,将其放入您的项目中 .

    • Say for Some Service "ComplexOperationService",实例化服务,获取 endpoints 端口并调用服务方法,并从Web服务获取响应:

    例如:

    ComplexOperationService service = new ComplexOperationService( );
    ComplexOperation port= service.getComplexOperationPort();    
    SomeComplexRequest request = --Get some complex request----;    
    SomeComplexResp resp = port.operate( request  );
    
    • 您不需要关心服务类/ req /响应类或任何其他类和方法,因为您知道它们都是从WSDL生成的 .

    • 当然你不需要知道soap action / envelop / namespace等 . 只需像我们一样调用方法,开发人员一直这样做 .

  • 2

    我认为Call SOAP Web Service from Android application会对你有所帮助 .

  • 3

    对我来说,最简单的方法是使用好的工具来生成所有必需的类 . 我个人使用这个网站:

    http://easywsdl.com/

    它支持相当复杂的Web服务并使用ksoap2 .

  • 6

    我相信你可以使用Axis创建一个小型SOAP客户端 . Axis installation instructions .

  • 2

    如果您在Android中调用Web服务有问题,那么您可以使用下面的代码来调用Web服务并获得响应 . 确保您的Web服务在 Data Table Format 中返回响应 . 如果您使用 SQL Server 数据库中的数据,此代码将为您提供帮助 . 如果您使用 MYSQL ,则需要更改一件事,只需将 obj2=(SoapObject) obj1.getProperty("NewDataSet"); 替换为 obj2=(SoapObject) obj1.getProperty("NewDataSet"); 11704

    void callWebService(){ 
    
    private static final String NAMESPACE = "http://tempuri.org/"; // for wsdl it may be package name i.e http://package_name
    private static final String URL = "http://localhost/sample/services/MyService?wsdl";
    // you can use IP address instead of localhost
    private static final String METHOD_NAME = "Function_Name";
    private static final String SOAP_ACTION = "urn:" + METHOD_NAME;
    
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("parm_name", prm_value);// Parameter for Method
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;// **If your Webservice in .net otherwise remove it**
        envelope.setOutputSoapObject(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    
        try {
            androidHttpTransport.call(SOAP_ACTION, envelope);// call the eb service
                                                                                                             // Method
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        // Next task is to get Response and format that response
        SoapObject obj, obj1, obj2, obj3;
        obj = (SoapObject) envelope.getResponse();
        obj1 = (SoapObject) obj.getProperty("diffgram");
        obj2 = (SoapObject) obj1.getProperty("NewDataSet");
    
        for (int i = 0; i < obj2.getPropertyCount(); i++) { 
    // the method getPropertyCount() and  return the number of rows
                obj3 = (SoapObject) obj2.getProperty(i);
                obj3.getProperty(0).toString();// value of column 1
                obj3.getProperty(1).toString();// value of column 2
                // like that you will get value from each column
            }
        }
    

    如果您对此有任何疑问,可以写信给我..

  • 15

    确实,由于它的开销,SOAP不是与移动设备进行数据交换的最佳选择 . 但是,您可能会发现自己处于无法控制服务器输出格式的情况 .

    所以,如果你必须坚持使用SOAP,那么在这里有一个为Android安装的kSOAP2库:
    http://code.google.com/p/ksoap2-android/

  • 12

    添加Soap Libaray( ksoap2-android-assembly-3.2.0-jar-with-dependencies.jar ):

    public static String Fn_Confirm_CollectMoney_Approval(

    HashMap < String, String > str1,
            HashMap < String, String > str2,
            HashMap < String, String > str3) {
    
        Object response = null;
        String METHOD_NAME = "CollectMoney";
        String NAMESPACE = "http://xxx/yyy/xxx";
        String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
        String SOAP_ACTION = "";
    
        try {
    
            SoapObject RequestParent = new SoapObject(NAMESPACE, METHOD_NAME);
    
            SoapObject Request1 = new SoapObject(NAMESPACE, "req");
    
            PropertyInfo pi = new PropertyInfo();
    
            Set mapSet1 = (Set) str1.entrySet();
    
            Iterator mapIterator1 = mapSet1.iterator();
    
            while (mapIterator1.hasNext()) {
    
                Map.Entry mapEntry = (Map.Entry) mapIterator1.next();
    
                String keyValue = (String) mapEntry.getKey();
    
                String value = (String) mapEntry.getValue();
    
                pi = new PropertyInfo();
    
                pi.setNamespace("java:com.xxx");
    
                pi.setName(keyValue);
    
                pi.setValue(value);
    
                Request1.addProperty(pi);
            }
    
            mapSet1 = (Set) str3.entrySet();
    
            mapIterator1 = mapSet1.iterator();
    
            while (mapIterator1.hasNext()) {
    
                Map.Entry mapEntry = (Map.Entry) mapIterator1.next();
    
                // getKey Method of HashMap access a key of map
                String keyValue = (String) mapEntry.getKey();
    
                // getValue method returns corresponding key's value
                String value = (String) mapEntry.getValue();
    
                pi = new PropertyInfo();
    
                pi.setNamespace("java:com.xxx");
    
                pi.setName(keyValue);
    
                pi.setValue(value);
    
                Request1.addProperty(pi);
            }
    
            SoapObject HeaderRequest = new SoapObject(NAMESPACE, "XXX");
    
            Set mapSet = (Set) str2.entrySet();
    
            Iterator mapIterator = mapSet.iterator();
    
            while (mapIterator.hasNext()) {
    
                Map.Entry mapEntry = (Map.Entry) mapIterator.next();
    
                // getKey Method of HashMap access a key of map
                String keyValue = (String) mapEntry.getKey();
    
                // getValue method returns corresponding key's value
                String value = (String) mapEntry.getValue();
    
                pi = new PropertyInfo();
    
                pi.setNamespace("java:com.xxx");
    
                pi.setName(keyValue);
    
                pi.setValue(value);
    
                HeaderRequest.addProperty(pi);
            }
    
            Request1.addSoapObject(HeaderRequest);
    
            RequestParent.addSoapObject(Request1);
    
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER10);
    
            soapEnvelope.dotNet = false;
    
            soapEnvelope.setOutputSoapObject(RequestParent);
    
            HttpTransportSE transport = new HttpTransportSE(URL, 120000);
    
            transport.debug = true;
    
            transport.call(SOAP_ACTION, soapEnvelope);
    
            response = (Object) soapEnvelope.getResponse();
    
            int cols = ((SoapObject) response).getPropertyCount();
    
            Object objectResponse = (Object) ((SoapObject) response)
                    .getProperty("Resp");
    
            SoapObject subObject_Resp = (SoapObject) objectResponse;
    
    
            modelObject = new ResposeXmlModel();
    
            String MsgId = subObject_Resp.getProperty("MsgId").toString();
    
    
            modelObject.setMsgId(MsgId);
    
            String OrgId = subObject_Resp.getProperty("OrgId").toString();
    
    
            modelObject.setOrgId(OrgId);
    
            String ResCode = subObject_Resp.getProperty("ResCode").toString();
    
    
            modelObject.setResCode(ResCode);
    
            String ResDesc = subObject_Resp.getProperty("ResDesc").toString();
    
    
            modelObject.setResDesc(ResDesc);
    
            String TimeStamp = subObject_Resp.getProperty("TimeStamp")
                    .toString();
    
    
            modelObject.setTimestamp(ResDesc);
    
            return response.toString();
    
        } catch (Exception ex) {
    
            ex.printStackTrace();
    
            return null;
        }
    
    }
    
  • 9

    我跟KSOAP约会了;我选择了一种相当简单的方法 .

    给定WSDL文件,为每个请求创建SOAP请求模板(例如:使用SOAP UI),然后替换要在代码中传递的值 . 使用DefaultHttpClient实例将此数据发布到服务 endpoints 并获取响应流 . 使用XML Pull解析器解析响应流 .

  • 8

    我为Android平台创建了一个新的SOAP客户端 . 它使用的是JAX-WS生成的接口,但到目前为止它只是一个概念验证 .

    如果您有兴趣,请尝试示例和/或在AndroidSOAP观看来源 .

  • 3

    请下载并添加带有项目的SOAP库文件文件名: ksoap2-android-assembly-3.4.0-jar-with-dependencies

    清理应用程序,然后启动程序

    这是SOAP服务调用的代码

    String SOAP_ACTION = "YOUR_ACTION_NAME";
        String METHOD_NAME = "YOUR_METHOD_NAME";
        String NAMESPACE = "YOUR_NAME_SPACE";
        String URL = "YOUR_URL";
        SoapPrimitive resultString = null;
    
        try {
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            addPropertyForSOAP(Request);
    
            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);
    
            HttpTransportSE transport = new HttpTransportSE(URL);
    
            transport.call(SOAP_ACTION, soapEnvelope);
            resultString = (SoapPrimitive) soapEnvelope.getResponse();
    
            Log.i("SOAP Result", "Result Celsius: " + resultString);
        } catch (Exception ex) {
            Log.e("SOAP Result", "Error: " + ex.getMessage());
        }
        if(resultString != null) {
            return resultString.toString();
        }
        else{
            return "error";
        }
    

    结果可能是JSONObject或JSONArray或String

    为了更好的参考,https://trinitytuts.com/load-data-from-soap-web-service-in-android-application/

    谢谢 .

  • 16

    大约一年前,我正在阅读这个线程,试图弄清楚如何在Android上进行SOAP调用 - 使用HttpClient构建我自己的建议导致我为Android构建自己的SOAP库:

    IceSoap

    基本上,它允许您构建通过简单Java API发送的信封,然后自动将它们解析为您通过XPath定义的对象...例如:

    <Dictionary>
        <Id></Id>
        <Name></Name>
    </Dictionary>
    

    变为:

    @XMLObject("//Dictionary")
    public class Dictionary {
        @XMLField("Id")
        private String id;
    
        @XMLField("Name")
        private String name;
    }
    

    我正在将它用于我自己的项目,但我认为它可能会帮助其他人,所以我花了一些时间将它分开并记录下来 . 我真的很喜欢它,如果你的一些可怜的灵魂在谷歌搜索“SOAP Android”时偶然发现这个线程可以给它一个机会并获得一些好处 .

  • 9

    不要忘记在项目中添加ksoap2.jar并在AndroidManifest文件中添加INTERNET权限

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class WebserviceActivity extends Activity {
    
        private static final String NAMESPACE = "https://api.authorize.net/soap/v1/";
        private static final String URL ="https://apitest.authorize.net/soap/v1/Service.asmx?wsdl"; 
        private static final String SOAP_ACTION = "https://api.authorize.net/soap/v1/AuthenticateTest";
        private static final String METHOD_NAME = "AuthenticateTest";
        private TextView lblResult;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            lblResult = (TextView) findViewById(R.id.tv);
    
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
            request.addProperty("name","44vmMAYrhjfhj66fhJN");
            request.addProperty("transactionKey","9MDQ7fghjghjh53H48k7e7n");
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try {
                androidHttpTransport.call(SOAP_ACTION, envelope);
    
                //SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
                // SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
                SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
    
    
                lblResult.setText(resultsRequestSOAP.toString());
                System.out.println("Response::"+resultsRequestSOAP.toString());
    
    
            } catch (Exception e) {
                System.out.println("Error"+e);
            }
    
        }
    }
    
  • 58

    如果你可以使用JSON,那么Developing Application Services with PHP Servers and Android Phone Clients中有一个白皮书,一个视频和sample.code .

相关问题