Uroš Podkrižnik Uroš Podkrižnik - 1 year ago 53
Android Question

Android: sending xml as document object, POST method

i am new at programming and i need some help with that please =/

web service is already written but not by me. so all i have to do is send xml as document object by post method through web service.

my code:

public class send extends application {

public void onCreate(Bundle icicle) {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://app.local/test/");

try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("packet");
rootElement.setAttribute("version", "1.2");

Element em = document.createElement("imei");

em = document.createElement("username");

HttpResponse response = httpclient.execute(httppost);

} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);


Answer Source

I am also very new in android programming. However, I solved those issued by using following way.

public void send(){

    DefaultHttpClient httpClient = new DefaultHttpClient();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("Servername", "abc"));
    nameValuePairs.add(new BasicNameValuePair("UserName", "123));
    nameValuePairs.add(new BasicNameValuePair("PassWord", "123"));
    nameValuePairs.add(new BasicNameValuePair("XML", getRequestTypeStringBuilder()));

    // Your every parameter name must be match with passing parameter, otherwise it throw 
    // an exception if it in case sensitive

    HttpPost httpPost = new HttpPost("http://app.local/DeviceLogin/");
    httpPost.addHeader("Accept", "text/xml");

    HttpResponse response   = httpClient.execute(httpPost);       

    // Be aware, if your return data type is also xml, then using replace empty string,
    // otherwise, it my not retrieve or seen all data.


private static String getRequestTypeStringBuilder(){
     StringBuilder body = new StringBuilder("<?xml version=\"1.0 \"encoding=\"UTF-8\"?>");
    body.append("<!DOCTYPE My System\"Abc.dtd\">");
    // Please append detail your xml body in here;
    return body.toString();

Hope this may solve your issue

"May all being be happy"

Regards and Metta, Ichirohang Limbu