2014-09-26 81 views
0

我想用代碼中的prepare xml調用soap webservice。但是所有webservice都調用參數的「add property」關鍵字。我期待發送所有xml塊獲取響應。調用soap webservice在android中準備xml

有人可以幫我嗎?

My soap xml;

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"> 
    <SOAP-ENV:Body> 
    <ns1:GetSessionTicketTypes> 
     <ns1:strUserName>thello</ns1:strUserName> 
     <ns1:strPassword>t123</ns1:strPassword> 
     <ns1:Cinema_strID>0000049</ns1:Cinema_strID> 
     <ns1:Session_strID>2374</ns1:Session_strID> 
    </ns1:GetSessionTicketTypes> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

回答

0

我找到了解決方案;

package com.example.testparse;

import java.io.IOException; 
import java.io.InputStream; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 

public class MainActivity extends Activity { 

    private final String URL = "Your URL"; 
    private final String SOAP_ACTION = "Soap URL"; 
    private final String METHOD_NAME = "GetSessionTicketTypes"; 

    String SOAPRequestXMLBody; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     /* 
     * if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy 
     * policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); 
     * StrictMode.setThreadPolicy(policy); } 
     */ 
     try { 
      makingEnvelopAndReady(URL, METHOD_NAME, "hellotest", "st!", 
        "00050", "95"); 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.v("Excepotuion in main method : ", 
        "Excepotuion in main method : " + e); 
     } 
    } 

    /* 
    * <strUserName>string</strUserName> <strPassword>string</strPassword> 
    * <Cinema_strID>string</Cinema_strID> <Session_strID>string</Session_strID> 
    */ 

    public String makingEnvelopAndReady(String URL, String Method, 
      String username, String password, String cinemaID, String sessionId) { 

     SOAPRequestXMLBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 
       + "<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>" 
       + "<GetSessionTicketTypes xmlns=\"http://tempuri.org/\">" 
       + "<strUserName>" + username + "</strUserName>" 
       + "<strPassword>" + password + "</strPassword>" 
       + "<Cinema_strID>" + cinemaID + "</Cinema_strID>" 
       + "<Session_strID>" + sessionId + "</Session_strID>" 
       + "</GetSessionTicketTypes>" + " </soap:Body>" 
       + "</soap:Envelope>"; 



     new RetrieveFeedTask().execute(); 

     return SOAPRequestXMLBody; 
    } 

    private byte[] callSOAPServer(String url, String soapAction, String envelope) { 

     byte[] result = null; 

     String temp; 

     HttpParams httpParameters = new BasicHttpParams(); 
     // Set the timeout in milliseconds until a connection is established. 
     int timeoutConnection = 35000; 
     HttpConnectionParams.setConnectionTimeout(httpParameters, 
       timeoutConnection); 
     // Set the default socket timeout (SO_TIMEOUT) 
     // in milliseconds which is the timeout for waiting for data. 
     int timeoutSocket = 55000; 
     HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 

     DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters); 

     /* 
     * httpclient.getCredentialsProvider().setCredentials(new 
     * AuthScope("os.icloud.com", 80, null, "Digest"), new 
     * UsernamePasswordCredentials(username, password)); 
     */ 
     HttpPost httppost = new HttpPost(url); 
     httppost.setHeader("soapaction", soapAction); 
     httppost.setHeader("Content-Type", "text/xml; charset=utf-8"); 

     System.out.println("executing request" + httppost.getRequestLine()); 
     // now create a soap request message as follows: 
     final StringBuffer soap = new StringBuffer(); 

     soap.append("" + envelope); 
     soap.append(""); 
     Log.i("SOAP Request", "" + soap.toString()); 

     try { 
      HttpEntity entity = new StringEntity(soap.toString(), HTTP.UTF_8); 
      httppost.setEntity(entity); 

      // Response handler 
       ResponseHandler rh=new ResponseHandler() { 
       // invoked when client receives response 
       public String handleResponse(HttpResponse response) 
        throws ClientProtocolException, IOException { 

       // get response entity 
       HttpEntity entity = response.getEntity(); 

       // read the response as byte array 
         StringBuffer out = new StringBuffer(); 
         byte[] b = EntityUtils.toByteArray(entity); 

         // write the response byte array to a string buffer 
         out.append(new String(b, 0, b.length));   
         return out.toString(); 
       } 
       }; 

       String responseString=httpclient.execute(httppost, rh); 
    /**************/   
       System.out.print(responseString); //result 


     } catch (Exception E) { 
      Log.i("Exception While Connecting", "" + E.getMessage()); 
      E.printStackTrace(); 
     } 

     httpclient.getConnectionManager().shutdown(); // shut down the 
                 // connection 
     return result; 
    } 

    class RetrieveFeedTask extends AsyncTask<Void, Void, Void> { 

     private Exception exception; 

     protected void onPostExecute(Void feed) { 
      // TODO: check this.exception 
      // TODO: do something with the feed 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 

      try { 
       callSOAPServer(URL, SOAP_ACTION, SOAPRequestXMLBody); 

      } catch (Exception e) { 
       this.exception = e; 

      } 
      // TODO Auto-generated method stub 
      return null; 
     } 
    } 
}