views:

3370

answers:

3

Hi,

I would like to invoke a webservice via Android. I need to POST some XML to a URL via HTTP. I found this snipped for sending a POST, but i dont know how to include/add the XML data itself.

public void postData() {
         // Create a new HttpClient and Post Header  
         HttpClient httpclient = new DefaultHttpClient();  
         HttpPost httppost = new HttpPost("http://10.10.4.35:53011/");

         try {  
             // Add your data  
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
             nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/soap+xml"));               
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                 // Where/how to add the XML data?


             // Execute HTTP Post Request  
             HttpResponse response = httpclient.execute(httppost);  

         } catch (ClientProtocolException e) {  
             // TODO Auto-generated catch block  
         } catch (IOException e) {  
             // TODO Auto-generated catch block  
         }  
     }

This is the complete POST message that i need to imitate:

POST /a8103e90-f1e3-11dd-bfdb-8b1fcff1a110 HTTP/1.1
Host: 10.10.4.35:53011
Content-Type: application/soap+xml
Content-Length: 602

<?xml version='1.0' encoding='UTF-8' ?>
<s12:Envelope xmlns:s12="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"&gt;
  <s12:Header>
    <wsa:MessageID>urn:uuid:fc061d40-3d63-11df-bfba-62764ccc0e48</wsa:MessageID>
    <wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/Get&lt;/wsa:Action&gt;
    <wsa:To>urn:uuid:a8103e90-f1e3-11dd-bfdb-8b1fcff1a110</wsa:To>
    <wsa:ReplyTo>
      <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous&lt;/wsa:Address&gt;
    </wsa:ReplyTo>
  </s12:Header>
  <s12:Body />
</s12:Envelope>
A: 

Here's my code for sending HTML.... You can see the data is the nameValuePairs.add(...)

        HttpClient httpclient = new DefaultHttpClient();
        // Your URL
        HttpPost httppost = new HttpPost("http://192.71.100.21:8000");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            // Your DATA
            nameValuePairs.add(new BasicNameValuePair("id", "12345"));
            nameValuePairs.add(new BasicNameValuePair("stringdata","AndDev is Cool!"));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response;
            response = httpclient.execute(httppost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
PLG
+1  A: 
  1. Firstly, you can create a String template for this SOAP request and substitute user-supplied values at runtime in this template to create a valid request.
  2. Wrap this string in a StringEntity and set its content type as text/xml
  3. Set this entity in the SOAP request. Something like:

    HttpPost httppost = new HttpPost(SERVICE_EPR);

    StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);

    se.setContentType("text/xml");

    httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");

    httppost.setEntity(se);

    BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient .execute(httppost);

    response.put("HTTPStatus",httpResponse.getStatusLine().toString());

Samuh
You should also consider reading this: http://stackoverflow.com/questions/297586/how-to-call-web-service-with-android/1940991#1940991
Samuh
Thanks that worked, much cleaner, although i had to a add a 'HttpClient httpclient = new DefaultHttpClient();'
Intosia
Of course, I just pasted the portion relevant to the point I was making... glad to know it helped.Cheers!
Samuh
A: 

I had to send some XML via HTTP Post on Android too.

String xml = "xml-block";
StringEntity se = new StringEntity(xml,"UTF-8");
se.setContentType("application/atom+xml");
HttpPost postRequest = new HttpPost("http://some.url");
postRequest.setEntity(se);

Hope it works!

Nushio