1
我嘗試使用不同的語言內容加載多個網站。只有俄羅斯的內容我已經看到了<?>
元素。請幫我把它解碼到正確的符號。我的代碼示例:如何解碼俄語
RequestTask t = new RequestTask();
response = t.doIt("http://google.ru"); //troubles
//response = t.doIt("http://stackoverflow.com"); //ok
//response = t.doIt("http://web.de/"); //ok
//response = t.doIt("http://www.china.com/"); // omg, it's ok too!
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String response_string = new String(out.toByteArray(), "UTF-8");
請求代碼:
public class RequestTask {
public HttpResponse doIt(String... uri)
throws ConnectTimeoutException, UnknownHostException, IOException{
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 6000);
HttpConnectionParams.setSoTimeout(params, 6000);
HttpClient httpclient = new DefaultHttpClient(params);
HttpResponse response = null;
Log.d(this.toString(), "HTTP GET to " + uri[0]);
response = httpclient.execute(new HttpGet(uri[0]));
Log.d(this.toString(), "response: " + response.getStatusLine().getReasonPhrase());
return response;
}
}
那麼,什麼是好的practicle辦呢?我如何確定頁面的編碼?現在我知道了兩種工作方法 - ''''''''''''''''''''''現在我知道了兩種工作方法 - 'String response_string = new String(out.toByteArray(),「windows-1251」);''String response_string = EntityUtils.toString(response.getEntity(),「UTF-8」 '。但是對於使用第一種方法,我需要確定響應的編碼,並且找不到合適的函數。第二種方法是有效的,但將它用於任何編碼是正確的? – psct
好的做法是你不知道頁面編碼,也沒有可靠的方法來找出答案。有時編碼將在'meta'標籤中指定,有時不會。查看答案更新以獲取更多代碼。 – lenik
但我可以找到像'unicode'這樣的在Java中引發編碼異常的函數?只有我找到的是'URL上的自動檢測編碼':http://illegalargumentexception.blogspot.ru/2009/05/java-rough-guide-to-character-encoding.html – psct