Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获得utf-8格式的html源代码?_Android_Post_Android Webview - Fatal编程技术网

Android 如何获得utf-8格式的html源代码?

Android 如何获得utf-8格式的html源代码?,android,post,android-webview,Android,Post,Android Webview,我编写这段代码是为了从站点获取html源代码 HttpURLConnection connection; OutputStreamWriter request = null; URL url = null; String response = null; String parameters = "aranan="+et.getText(); try { ur

我编写这段代码是为了从站点获取html源代码

        HttpURLConnection connection;
        OutputStreamWriter request = null;

        URL url = null;
        String response = null;
        String parameters = "aranan="+et.getText();

        try
        {
            url = new URL("http://www.fragmanfan.com/arama.asp");
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            request = new OutputStreamWriter(connection.getOutputStream());
            request.write(parameters);
            request.flush();
            String line = "";
            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            // Response from server after login process will be stored in response variable.
            response = sb.toString();
            // You can perform UI operations here
            browser.loadDataWithBaseURL(null, response,"text/html", "UTF-8", null);

            isr.close();
            reader.close();
        }
        catch(IOException e)
        {
            // Error
        }
    }
});
但有一个问题:response(包含html源代码的变量)不是utf-8格式。 我怎样才能解决这个问题?
谢谢。

因为您的响应似乎是一个字符串中的HTML网页,所以您应该确保页面的头标签包含定义编码的标签。。如果没有,您可以自己将其附加到StringBuilder。 以下是您如何做到这一点:

final StringBuilder sb = 
new StringBuilder("<html><head>"+ "<meta http-equiv=\"content-type\"content=\"text/html;charset=utf-8\" />"+ "</head><body>");
while ((line = reader.readLine()) != null) {
    sb.append(line + "\n");
    }
response = sb.toString();
sb.append(response);
sb.append("</body></html>");
最终StringBuilder sb=
新的StringBuilder(“+”+”);
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
response=sb.toString();
某人追加(答复);
某人加上(“”);
然后您可以将HTML正确加载到webview/浏览器中。(这对我很有效,因此我确信它确实有效=])

p、 确保接受正确回答你问题的答案,这样人们会继续回答你未来的问题。

。 .

。 .
.

可能InputStreamReader isr=新的InputStreamReader(connection.getInputStream(),“utf-8”);它不起作用,因为问题是:行不是utf-8格式,我的意思是sb.append(行+“\n”);这行返回例如像这样的hal?(可以是halı)。您先写,但这并不意味着reader.readline返回不是utf-8字符。我如何修复它
 InputStreamReader isr = new InputStreamReader(connection.getInputStream(),"ISO-8859-9");