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>

您可以看到,在我的请求中,我的服务需要"肥皂"字。

任何人都可以帮助。

很高兴您问了这个问题,当我开始使用SOAP WebService时,我遇到了同样的问题。这里的关键是避免使用SOAP库,然后去Java提供的课程来提出请求并解析,即HTTP,DOM Parser或SAX Parser。这就是您在不使用KSOAP或任何其他库的情况下提出请求的方式。

现在进入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();
                }
            }

现在,在您的创建中,只需继续使用以下代码

执行此类
runTask task = new runTask();
task.execute();

您将在OnPostexecute,格式化并从这里分析它。使用此无库方式的主要优点是它是灵活的,您可以以任何仅使用提供的请求格式的库以任何方式格式化请求。该解决方案在我的代码中无缝工作,随时要求进一步澄清。

相关内容

  • 没有找到相关文章

最新更新