开发者

Chunked web service response

开发者 https://www.devze.com 2023-02-15 13:20 出处:网络
can you please provide me with an example on how to read a chuncked response from a web service in Android

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 = n开发者_如何学JAVAew 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);
                    }


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消