0

I'm requesting some content from my website (in my Android app) using the following code:

response = httpClient.execute(httpPost, localContext);


// Pull content stream from response
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
InputStreamReader rd = new InputStreamReader(inputStream);
//ByteArrayOutputStream content = new ByteArrayOutputStream();
StringBuilder content = new StringBuilder();
int n = 0;
char[] buffer = new char[40000];
while (n >= 0)
{
        n = rd.read(buffer, 0, buffer.length);
        if (n > 0)
        {
            content.append(buffer, 0, n);
        }
}
ret = content.toString();

The content of the site is in encoded in the Windows-1251 encoding, but in the app I get a bunch of unreadable symbols, like so:

enter image description here

Also, the following code snippet doesn't seem to be working either:

text = new String(matcher.group(1).getBytes("Windows-1251"), "UTF-8");

enter image description here

1 Answer 1

1

Try this ,

    BufferedReader reader = new BufferedReader(new InputStreamReader(in_stream), "UTF-8");
    StringBuilder content = new StringBuilder();
    String line = null;
    try
    {
        while ((line = reader.readLine()) != null) 
        {
            content.append(line + "\n");
        }

    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try 
        {
            in_stream.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
    ret = content.toString();
Sign up to request clarification or add additional context in comments.

1 Comment

new InputStreamReader(in_stream, "UTF-8"), not new BufferedReader(rd, "UTF-8").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.