2

Hi and thanks in advance. I found this tutorial and I'm trying to emulate it: http://lalit3686.blogspot.com/2012/06/calling-soap-webservice-using-httppost.html. In my iOS app I'm able to create a string with xml properties, send it as a SOAP request, and get an xml response in string form. I'm trying to do the same thing with my android app.

Unfortunately something isn't working. I've attached relevant code.

    StringBuilder stringBuilder = new StringBuilder ();
    stringBuilder.append(String.format("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
            "xmlns:csr=\"http://www.mywebsite.net/\">\n" +
            "<soapenv:Header/>\n" +
            "<soapenv:Body>\n" +
            "<csr:www.mywebsite.net><![CDATA[\n" +
            "<www.mywebsite.net>\n" +
            "<Request>\n" +
            "<user>%s</user>\n" +
            "<password>%s</password>\n" +
            "<Function>requestdata</Function>\n" +
            "<FromDate>2013-01-01</FromDate>\n" +
            "<ToDate>2014-01-01</ToDate>\n" +
            "</Request>\n" +
            "</www.mywebsite.net>\n" +
            "]]></csr:www.mywebsite.net>\n" +
            "</soapenv:Body>\n" +
            "</soapenv:Envelope>\n", username, password));

    String xml = stringBuilder.toString();
    HttpPost httpPost = new HttpPost("http://www.mywebsite.net/");
    StringEntity entity;
    String response_string = null;

    try {

        entity = new StringEntity(xml, HTTP.UTF_8);
        httpPost.setHeader("Content-Type","text/xml;charset=UTF-8");
        httpPost.setEntity(entity);
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        response_string = EntityUtils.toString(response.getEntity());
        Log.d("request", response_string);
        prefs.edit().putString("response", response_string).commit();

    } 

    catch (Exception e) {
        e.printStackTrace();
    }

Edit: problem fixed. I was getting the error:

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).

The code above works - just make sure to include this code in your onCreate() method.

 if (android.os.Build.VERSION.SDK_INT > 9) {
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);
 }

This allows you to use the main thread for SOAP requests/responses.

Michael E
  • 656
  • 2
  • 7
  • 13

3 Answers3

3

I was getting the error:

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099).

I googled this error and came upon this link: DefaultHttpClient to AndroidHttpClient - which suggested I include the following code in my onCreate method.

if (android.os.Build.VERSION.SDK_INT > 9) {
  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  StrictMode.setThreadPolicy(policy);
}

Now its working. In fact, Android does not let network operations be performed on the main thread any longer.

Community
  • 1
  • 1
Michael E
  • 656
  • 2
  • 7
  • 13
1

This is because you run network operation on UI thread which violates Strict Mode. Use AsyncTask for background operation.

0

http://www.youtube.com/watch?v=v9EowBVgwSo. The video has a tutorial of how to go about sending a soap webservice request in android.

http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data. The link here has an example with explanation. Go through the links and modify your code accordingly.

Sending request in xml form. I had a similar requirement and i used the below format to sent a soap reuest in xml form. Modify the below according to your needs. I

PropertyInfo loginreq = new PropertyInfo();
loginreq.name="LoginReq";
loginreq.type=String.class;
loginreq.setValue("<LoginReq>" 
     +"<nickName>"+name+"</nickName>" 
     +"<password>"+pass+"</password>" 
     +"<IMEI>"+mImei+"</IMEI>" 
     +"<location>" 
     +"<latitude>"+lati+"</latitude>"
         +"<longitude>"+longi+"</longitude>"
     +"</location>" 
     +"</LoginReq>");
request.addProperty(loginreq);

Android, sending XML via HTTP POST (SOAP). Have a look at the accepeted answer in the link.

Edit

http://www.ibm.com/developerworks/opensource/library/x-android/. Have a look at the heading Creating XML.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • the line response.put("HTTPStatus",httpResponse.getStatusLine().toString()); gives me an error... i'm tinkering with it at the moment. – Michael E Mar 15 '13 at 06:28
  • try the above code by adding property to the request.http://developer.android.com/training/articles/perf-tips.html#ObjectCreation. You have too many strings in your code. have a look at the link under the heading Avoid Creating Unnecessary Objects. – Raghunandan Mar 15 '13 at 06:33
  • is your response also in xml format. What is the error that your getting?? – Raghunandan Mar 15 '13 at 06:44
  • i'm expecting a response in xml format - but i was planning on parsing through it as a string as i did in my ios app – Michael E Mar 15 '13 at 07:01
  • so u r getting response?? u need to parse that response now?? – Raghunandan Mar 15 '13 at 08:00
  • yeah - i think i'll design my own code to parse the response though - thanks for the suggestions – Michael E Mar 15 '13 at 16:55