1

I'm trying to make a connection to a URL which displays some data in plain text on a webpage (it's a block of text in between <pre> tags). Whenever a character with a character with a special character is passed through (for example é or ì) it displays a question mark instead of the character a � is displayed. I've tried using this but that didn't change much. I can't read the page per line so I'm pretty much stuck here.

Here is my code:

 try
        {
            // Open the connection 
            NetworkManager networkManager = NetworkManager.getInstance();
            networkManager.addErrorListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    NetworkEvent n = (NetworkEvent) evt;
                    n.getError().printStackTrace();
                    success = false;
                }
            });
            ConnectionRequest request = new ConnectionRequest()
            {               
                {
                    setTimeout(40000);
                }

            @Override
            protected void readResponse(InputStream input) throws IOException
            {
                InputStreamReader inputStreamReader = new InputStreamReader(input, "UTF-8");
                int chr;
                buffer = new StringBuilder();
                //reading the answer                     
                while ((chr = inputStreamReader.read()) != -1)
                {
                    System.out.println("Append the character " + (char) chr);
                    buffer.append((char) chr);
                }
                response = buffer.toString();
                response = response.trim();

                data = new byte[buffer.length()];
                int tmpCounter = buffer.length();
                counter = 0;                    
                while (counter != buffer.length())
                {
                    for (int i = 0; i < tmpCounter; i++)
                    {
                        data[counter + i] = (byte) buffer.charAt(i);
                    }
                    counter += tmpCounter;
                }
                process(data);
                dataEvent.setDataAvailable();
            }

            @Override
            protected void handleException(Exception err)
            {
                success = false;
                dataEvent.setDataAvailable();
            }
        };
        request.setUrl(url);
        request.setPost(false);
        networkManager.addToQueue(request);
    }
    catch (Exception x)
    {
        x.printStackTrace();
    }
Rodin10
  • 327
  • 3
  • 15

1 Answers1

0

Turns out the actual response didn't respond using UTF-8 the characters were just readable in my browser. I fixed it by specifying the response characters should be UTF-8

Rodin10
  • 327
  • 3
  • 15