Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ksoap2请求格式错误-_Android_Web Services_Soap_Wsdl_Ksoap2 - Fatal编程技术网

Android ksoap2请求格式错误-

Android ksoap2请求格式错误-,android,web-services,soap,wsdl,ksoap2,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> <inser

这是我的要求:

<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>

gmg
gmg
gmg
gmg
gmg
rssi Ejempo
我需要像那样的服务

<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”两个字


任何人都可以帮助我。

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

现在转到Android代码:

我们将创建一个名为runTask的类,该类扩展了async task,并使用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();
                }
            }
您将在onPostExecute中获得响应,从这里格式化并解析它。 使用这种无库方式的主要优点是它非常灵活,与只使用提供的请求格式的库相比,您可以按照Web服务要求的任何方式格式化请求。此解决方案在我的代码中无缝工作,请随时要求进一步澄清

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