ksoap2请求错误形成 -

时间:2017-03-05 21:40:23

标签: android web-services soap wsdl ksoap2

这是我的要求:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Body>
<insertBeacons xmlns="http://tempuri.org/insertBeacons/">
<MAC_ADDRESS>gmg</MAC_ADDRESS>
<UUID>gmg</UUID>
<MAJOR>gmg</MAJOR>
<MINOR>gmg</MINOR>
<MEASURED_POWER>gmg</MEASURED_POWER>
<RSSI>rssi ejemplo</RSSI>
</insertBeacons>
</v:Body>
</v:Envelope>

我需要发送到那样的服务

<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>
    <insertBeacons xmlns="http://tempuri.org/">
      <MAC_ADDRESS>string</MAC_ADDRESS>
      <UUID>string</UUID>
      <MAJOR>string</MAJOR>
      <MINOR>string</MINOR>
      <MEASURED_POWER>string</MEASURED_POWER>
      <RSSI>string</RSSI>
    </insertBeacons>
  </soap:Body>
</soap:Envelope>

你能看到,在我的请求中是“v”而我的服务需要“肥皂”字。

任何人都可以提供帮助。

1 个答案:

答案 0 :(得分:0)

很高兴你问这个问题,当我开始使用soap webservice时,我遇到了同样的问题。这里的关键是避免使用soap库,并使用java提供的类来发出请求并解析它,即http,DOM解析器或SAX解析器。这是您在不使用kso​​ap或任何其他库的情况下提出请求的方式。

现在开始使用androiod代码:

我们将创建一个名为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.


                    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>"+
                                    "<insertBeacons xmlns="http://tempuri.org/">"+
                                    "<MAC_ADDRESS>"+string+"</MAC_ADDRESS>"+
                                    "<UUID>"+string+"</UUID>"+
                                    "<MAJOR>"+string+"</MAJOR>"+
                                    "<MINOR>"+string+"</MINOR>"+
                                    "<MEASURED_POWER>"+string+"</MEASURED_POWER>"+
                                    "<RSSI>"+string+"</RSSI>"+
                                    "</insertBeacons>"+
                                    "</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);
                        //you can pass all your request parameters here usong .setRequestProperty() method

                        //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();

                  //Go ahead and parse the response now

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

现在在onCreate中,继续使用以下代码执行此类

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

您将在onPostExecute中获得响应,格式化并从此处解析。 使用这种无库文件方式的主要优点是它非常灵活,与只使用提供的请求格式的库相比,您可以以Web服务所需的任何方式格式化请求。该解决方案可以在我的代码中无缝运行,随时可以要求进一步澄清。