2017-03-05 77 views
0

這是我的要求:ksoap2請求錯誤formed-

<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」和我的服務需要「肥皂」字樣。

任何人都可以幫助。

回答

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服務所需的格式與僅使用所提供的請求格式的庫相比較。這個解決方案可以在我的代碼中無縫工作,隨時請求進一步澄清。