【问题标题】:ksoap2 request wrong formed-ksoap2 请求格式错误-
【发布时间】:2017-03-05 21:40:22
【问题描述】:

这是我的要求:

<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”的,而我的服务需要“soap”这个词。

任何人都可以帮助我。

【问题讨论】:

    标签: android web-services soap wsdl ksoap2


    【解决方案1】:

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

    现在进入安卓代码:

    我们将创建一个名为 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 服务所需的任何方式格式化请求。此解决方案在我的代码中无缝运行,如有任何进一步的说明,请随时询问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多