hi4ppl hi4ppl - 2 months ago 12
Java Question

Arabic RSS can't be read in java it gives me symbols

I'm trying to read

RSS
feed from a
URL
in java but I don't get Arabic output just bunch of symbols, below is a sample code, it works with English but does not with Arabic...
I tried couple of examples from web and could not solve it.

public static void main(String[] args) {

try {
URL cali = new URL(
"http://services.explorecalifornia.org/rss/tours.php");
URL aljazera = new URL(
"http://www.aljazeera.net/aljazeerarss/3c66e3fb-a5e0-4790-91be-ddb05ec17198/4e9f594a-03af-4696-ab98-880c58cd6718");
InputStream stream = aljazera.openStream();
BufferedInputStream buf = new BufferedInputStream(stream);
StringBuilder sb = new StringBuilder();

while (true) {
int data = buf.read();

if (data == -1) {
break;
} else {

sb.append((char) data);

}

}

System.out.println(sb);

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

Answer

Try to specify the encoding type of the InputStream:

InputStreamReader isr = new InputStreamReader(stream, "UTF-8");
StringBuilder sb = new StringBuilder();

    while (true)
    {
        int data = isr.read();

        if (data == -1) {
            break;
        } else {

            sb.append((char) data);

        }

    }
Comments