I
I
Ilya Chernichkin2011-09-15 19:43:23
Java
Ilya Chernichkin, 2011-09-15 19:43:23

java encoding from windows-1251 to utf-8

Hello everyone
Please tell me
There is a site in windows-1251 encoding I do the
following
try {
URL u = new URL("sitename");
URLConnection conn = u.openConnection();
DataInputStream in = new DataInputStream ( conn.getInputStream ( ) ) ;
BufferedReader d = new BufferedReader(new InputStreamReader(in));
String str=null;
StringBuilder sb = new StringBuilder();
while( (str = d.readLine()) !=null)
{
sb.append(str);
}
str = sb.toString();
d.close();

WebView myWebView = (WebView) findViewById(R.id.webview);
int st=str.indexOf("block_title")-12;
int en=str.indexOf("block2")-18;
String res=str.substring(st,en);
String utf8String= new String(res.getBytes("UTF-8"), "windows-1251");;
String summary = "!"+utf8String+"";
myWebView.loadData(summary, "text/html", "utf-8");

и в WebView выходят каракули
подскажите как конвертнуть

Спасибо

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
apangin, 2011-09-15
@INCWADRA

You are reading from the stream in the wrong encoding.
We need to create the correct InputStreamReader:

InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "windows-1251");
BufferedReader br = new BufferedReader(isr);

K
korvindest, 2011-09-15
@korvindest

In Java, the default encoding is UTF-8, so when you write:

String utf8String= new String(res.getBytes("UTF-8"), "windows-1251");

You are actually translating UTF-8 to windows-1251 and displaying as UTF-8.
You can easily check my guess using the Stirlitz 4.0 program

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question