Answer the question
In order to leave comments, you need to log in
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
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);
In Java, the default encoding is UTF-8, so when you write:
String utf8String= new String(res.getBytes("UTF-8"), "windows-1251");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question