【问题标题】:Connection for android and SOAP apiandroid 和 SOAP api 的连接
【发布时间】:2017-03-07 12:14:30
【问题描述】:

我是 SOAP 的新开发人员,所以请指导我如何在 android 中调用 soap api。 这是我找到的方法,但不知道它是如何工作的。我已经下载了this

 public static String connectSOAP(String url, String soapNamespace, String soapAction, String soapMethod, Hashtable<String, String> HT) {

        String result = null;

        SoapObject request = new SoapObject(soapNamespace, soapMethod);

        if (HT != null) {
            Enumeration<String> en = HT.keys();
            while (en.hasMoreElements()) {

                Object k = en.nextElement();
                Object v = HT.get(k);
                request.addProperty(k.toString(), v);
                System.out.println("key = " + k.toString() + "; value = " + v);
            }
        }


        SoapSerializationEnvelope envelope =
                new SoapSerializationEnvelope(SoapEnvelope.VER11);

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
        //envelope.getResponse();


        try {

            androidHttpTransport.call(soapAction, envelope);

            if (envelope.bodyIn instanceof SoapFault) {

                result = ((SoapFault) envelope.bodyIn).faultstring;


            } else {

                SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
                result = resultsRequestSOAP.getProperty(0).toString();
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

这是我想用作 api 调用但不知道如何使用它的方法。 所以请帮助我。

【问题讨论】:

  • Here你会得到一步一步的指导。
  • 好的,谢谢@sodhankit。如果我有任何问题,我会回到这一点。

标签: android api soap


【解决方案1】:

在使用soap 网络服务时,我强烈建议您不要使用任何库来发出soap 请求。使用 android 提供的类更方便,更不容易出错。这就是您在不使用库的情况下发出soap 请求的方式:首先,您需要知道如何使用SOAP Ui,它是一个Windows 应用程序。您可以在此处导入您的 wsdl 文件,如果我们的语法正确,那么您将看到一个屏幕,显示您的 Web 服务的请求正文。您可以输入测试值,您将获得一个响应结构。此链接将指导您如何使用soap ui https://www.soapui.org/soap-and-wsdl/working-with-wsdls.html

现在进入安卓代码:

我们将创建一个名为 runTask 的类,它扩展异步任务并使用 http 发送请求正文并获取请求响应:

 private class runTask extends AsyncTask<String, String, String> {

        private String response;
        String string = "your string parameter"
        String SOAP_ACTION = "your soap action here";

        String stringUrl = "http://your_url_here";
        //if you experience a problem with url remove the '?wsdl' ending



        @Override
        protected String doInBackground(String... params) {

            try {

                        //paste your request structure here as the String body(copy it exactly as it is in soap ui)
                        //assuming that this is your request body


                String body = "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" +
                                "<soap:Body>"+
                                "<GetCitiesByCountryResponse xmlns="http://www.webserviceX.NET">"+
                                "<GetCitiesByCountryResult>"+string+"</GetCitiesByCountryResult>"+
                                "</GetCitiesByCountryResponse>"+
                                "</soap:Body>"+
                                "</soap:Envelope>";


                try {
                    URL url = new URL(stringUrl);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setDefaultUseCaches(false);
                    conn.setRequestProperty("Accept", "text/xml");
                    conn.setRequestProperty("SOAPAction", SOAP_ACTION);

                    //push the request to the server address

                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
                    wr.write(body);
                    wr.flush();

                    //get the server response

                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line = null;

                    while ((line = reader.readLine()) != null) {


                        builder.append(line);
                        response = builder.toString();//this is the response, parse it in onPostExecute

                    }


                } catch (Exception e) {

                    e.printStackTrace();
                } finally {

                    try {

                        reader.close();
                    } catch (Exception e) {

                        e.printStackTrace();
                    }
                }


            } catch (Exception e) {

                e.printStackTrace();
            }

            return response;
        }

        /**
         * @see AsyncTask#onPostExecute(Object)
         */
        @Override
        protected void onPostExecute(String result) {



           try {

              Toast.makeText(this,"Response "+ result,Toast.LENGTH_LONG).show();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

现在只需执行该类并观看魔术发生:

runTask runner = new runTask();
runner.execute();

得到响应后,您可以使用 DOM 或 SAX 解析对其进行解析,稍作研究,您就会了解如何解析。您可以使用 java 和最好的 Eclipse Ide 创建 WSDL web 服务。教程也可以在 youtube 上的 WSDL 网络服务上找到。

如果解决方案不清楚,请随时要求澄清。

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多