ksoap2请求错误形成- [英] ksoap2 request wrong formed-

查看:90
本文介绍了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"的字样,而我的服务需要带有"soap"字样.

can you see, in my request is with "v" and my service need "soap" word.

任何人都可以帮忙.

推荐答案

很高兴你问了这个问题,当我开始使用soap webservice时,我也遇到了同样的问题.此处的关键是避免使用soap库,而是使用java提供的用于发出请求并对其进行解析的类,即http,DOM解析器或SAX解析器.这样就可以在不使用kso​​ap或任何其他库的情况下发出请求.

Glad you asked this question, i faced the same problem when i started out with soap webservice. The key here is to avoid using soap libraries and go for the classes that java has provided to make the request and parse it i.e http,DOM parser or SAX parser. This is how you make your request without using ksoap or any other libraries.

现在继续执行雄性代码:

Now on to the androiod code :

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

We will create a class named runTask which extends async task and use http to send the request body and get request response :

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中,继续执行下面的代码并执行该类

Now in your onCreate just go ahead and execute this class using the following code

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

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

You will get your response in your onPostExecute, format and parse it from here. The main advantages of using this libraryless way is that it is flexible, you can format the request in any way the webservice requires as compared to the libraries which you only use the provided request formats. This solution works seamlessly in my code, feel free to ask for any further clarification.

这篇关于ksoap2请求错误形成-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆