Google recommends to use ResponseHandler for parsing contents of the
entity, so I use the following code.
Also there you can easily handle compressed (gziped) contents.
As for encoding, first you should try to extract encoding information
from the header.

        private ResponseHandler<String> responseHandler = new
ResponseHandler<String>()
        {
                @Override
                public String handleResponse(HttpResponse response)
                                throws ClientProtocolException, IOException
                {
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() >= 300)
                {
                    throw new
HttpResponseException(statusLine.getStatusCode(),
                            statusLine.getReasonPhrase());
                }

                HttpEntity entity = response.getEntity();
                if(entity == null)
                        return null;

                InputStream instream = entity.getContent();
                Header contentEncoding = response.getFirstHeader("Content-
Encoding");
                if (contentEncoding != null &&
contentEncoding.getValue().equalsIgnoreCase("gzip"))
                {
                        instream = new GZIPInputStream(instream);
                }
                return entity == null ? null : this.toString(entity,
instream, "UTF-8");
                }

                /**
                 * Reads entity input stream into String with the given 
encoding.
                 * Base code taken from org.apache.http.util.EntityUtils and
slightly corrected
                 * @param entity
                 * @param instream
                 * @param defaultCharset
                 * @return
                 * @throws IOException
                 * @throws ParseException
                 */
                public String toString(
                                final HttpEntity entity,
                                final InputStream instream,
                                final String defaultCharset) throws 
IOException, ParseException
                {
                        if (entity == null)
                        {
                                throw new IllegalArgumentException("HTTP entity 
may not be null");
                        }

                        if (instream == null)
                        {
                                return null;
                        }
                        if (entity.getContentLength() > Integer.MAX_VALUE)
                        {
                                throw new IllegalArgumentException("HTTP entity 
too large to be
buffered in memory");
                        }
                        int i = (int)entity.getContentLength();
                        if (i < 0)
                        {
                                i = 4096;
                        }
                        String charset = EntityUtils.getContentCharSet(entity);
                        if (charset == null)
                        {
                                charset = defaultCharset;
                        }
                        if (charset == null)
                        {
                                charset = HTTP.DEFAULT_CONTENT_CHARSET;
                        }

                        Reader reader = new InputStreamReader(instream, 
charset);

                        StringBuilder buffer=new StringBuilder(i);
                        try
                        {
                                char[] tmp = new char[1024];
                                int l;
                                while((l = reader.read(tmp)) != -1)
                                {
                                        buffer.append(tmp, 0, l);
                                }
                        } finally
                        {
                                reader.close();
                        }

                        return buffer.toString();
                }
        };

        protected String doGET(HttpClient cln, String url) throws IOException
        {
                        HttpGet get = new HttpGet(url);
                        setHttpHeader(get); //setup our request header data
                        String responseS = null;
                        try
                        {
                                responseS = cln.execute(get, new 
ResponseHandler<String>());
                                return responseS;
                        }
                        catch(IOException iex)
                        {
                                //do some clean-up and rethrow
                                throw iex;
                        }
       }


On Oct 2, 11:11 pm, Bob Kerns <[email protected]> wrote:
> One other little flaw there -- possibly serious -- is not specifying the
> encoding in the new InputStreamReader(stream, encoding) call.
>
> Generally, you should use UTF-8 if you have a choice -- and you should use
> what was supplied by the server, in any event. If you're writing the server,
> make it UTF-8 on that end.
>
> And you should use StringBuilder rather than  StringBuffer - a more
> efficient replacement. StringBuffer has synchronized methods, which are
> wasteful when you're using it from a single thread, as here, and in the vast
> majority of cases.
>
> And rather than using BufferedReader and readLine, it is more efficient to
> allocate a single char[] array, and use that in a loop:
>
> HttpEntity entity = response.getEntity();
> Reader in = new InputStreamReader(entity.getContent(),
> entity.getContentEncoding());
> try {
>     StringBuilder sb = new StringBuilder();
>     char[] buf = new char[1024];
>     int count = 0;
>     while ((count = in.read(buf)) >= 0) {
>         sb.append(buf, 0, count);
>     }
>    return sb.toString();
>
>
>
>
>
>
>
> } finally {
>   in.close();
> }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to