2

can you please provide me with an example on how to read a chuncked response from a web service in Android

thanks

Edit: I'm try to call a soap web service, that replies to me with a base64 encoded string representing an image

here's the code:

String SOAP_ACTION = "service soap action";
         try {
            URL u = new URL("server url");
            URLConnection uc = u.openConnection();
            HttpURLConnection connection = (HttpURLConnection) uc;
            connection.setDoOutput(true);
               connection.setDoInput(true);
               connection.setRequestProperty("SOAPAction", SOAP_ACTION);
               connection.setRequestMethod("POST");
               connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
String xmldata="soap request envelope";
//send the request
               OutputStream out = connection.getOutputStream();

                  Writer wout = new OutputStreamWriter(out);

                  wout.write(xmldata);

                  wout.flush();

                  wout.close();

                  BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));



                    String result;
                    StringBuilder builder=new StringBuilder();
                            //read response
                    while ((result=rd.readLine()) != null) {
                        builder.append(result);
                    }
Reno
  • 33,594
  • 11
  • 89
  • 102
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158

3 Answers3

1

Preachy Answers:

  1. You should really use a SOAP library rather than trying to re-invent the wheel: How to call a SOAP web service on Android
  2. If possible use a REST service rather than SOAP since SOAP is not suited for mobile platforms: http://javatheelixir.blogspot.com/2009/12/soap-vs-rest-in-service-layer-for.html

Practical Answer:

Problem:

  • It seems you are only getting the first chunk of the response.

Possible solutions

  1. Try write(int c) instead of write(String str). Then ignore \r and \n characters.
  2. Use read() in a loop instead of readLine().

Note to questioner: Apologies for leaving in the Preachy answers. I am sure that you have considered those options. But it will help people who are able to use a SOAP library. If you have decided not to use a SOAP library for a specific reason please put it in the comments for the benifit of others.

Community
  • 1
  • 1
Mandar Limaye
  • 1,900
  • 16
  • 24
0

Try to use more generalized classes, such as DefaultHttpClient and HttpPost to handle HTTP-interaction in a more higher level.

Olegas
  • 10,349
  • 8
  • 51
  • 72
  • What you are getting? Response body with "chunk" spliters? Can you show it? – Olegas Mar 30 '11 at 11:09
  • No I'm getting incomplete response body, the response is a soap response. something like this ?xml version="1.0" encoding="utf-8"?>iVBOR and the closing tags are missing – Mina Wissa Mar 30 '11 at 11:25
  • Is DefaultHttpClient+HttpPost giving the same result? – Olegas Mar 30 '11 at 11:49
  • I think you are already checked it thousand times, but... is your service returning a correct result? Can you emulate a call (using Fiddler or smth else) and get a valid response? – Olegas Mar 30 '11 at 12:01
  • Yes, I downloaded a web service client app. tested the service, the response is correct, but when called from android app it returns an incomplete response – Mina Wissa Mar 30 '11 at 12:07
0

You do not want to use the urlconnection class. You'll want to use the httpclient bundled with android like follows.

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("http://something.com/something");
HttpResponse response = client.execute();

int code = response.getStatusLine().getStatusCode();
if(code == 200){
   InputStream is = response.getEntity().getContent();

   //Now parse the xml coming back
   SAXParserFactory spf = SAXParserFactory.newInstance();
   SAXParser sp = spf.newSAXParser();

   XMLReader xr = sp.getXMLReader();
   YourParser parser = new YourParser();

   xr.setContentHandler(parser);

   xr.parse(new InputSource(is));
}

You should simply need to create your xml parser to parse your objects. Hope this helps.

Brian Griffey
  • 4,751
  • 1
  • 25
  • 26