Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.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
Java Android HTTP请求编码_Java_Android_Encoding_Utf 8_Httprequest - Fatal编程技术网

Java Android HTTP请求编码

Java Android HTTP请求编码,java,android,encoding,utf-8,httprequest,Java,Android,Encoding,Utf 8,Httprequest,我想使用以下代码在我的Android应用程序中执行HTTPRequest: BufferedReader in = null; try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI("http://www.example.de/example.php")); Http

我想使用以下代码在我的Android应用程序中执行HTTPRequest:

BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("http://www.example.de/example.php"));
        HttpResponse response = client.execute(request);
        in = new BufferedReader
        (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();

        System.out.println(page);
        return page;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                    e.printStackTrace();
            }
        }
   }
我调用的网页是一个返回字符串的php脚本。我的问题是,特殊字符ä、ü、ö、欧元等显示为带方框的问号。我怎样才能得到这些字符


我认为这是编码德语应用程序->UTF-8的问题?

可能是您可以在控制台中显示时尝试设置编码。某些字符从服务器正确返回,但无法在控制台中显示

String page = sb.toString();
PrintStream out = new PrintStream(System.out, true, "UTF-8");
out.println(page);

我已经玩弄了你的代码http://www.google.de.

我能够破解一些东西,但不确定它是否是最优雅的解决方案

行后:

HttpResponse response = client.execute(request);
。。。我补充说:

HttpEntity e = response.getEntity();
Header ct = e.getContentType();
HeaderElement[] he = ct.getElements();
if (
    he.length > 0 
        && he[0].getParameters().length > 0
        && he[0].getParameter(0) != null 
        && he[0].getParameter(0).getName().equals("charset")
    ) {
    String charset = he[0].getParameter(0).getValue();
    // with google.de, will print ISO latin ("ISO-8859-1")
    Log.d("com.example.test", charset);
}
。。。然后,您可以添加字符集表示或其Java等价物作为InputStreamReader构造函数调用的第二个参数:

in = new BufferedReader(
    new InputStreamReader(
        response.getEntity().getContent(), 
        charset != null ? charset : "UTF-8"
);
如果这对你有效,请告诉我


还要注意,为了检查Java字符集的等价性,可以使用charset.forNameString charsetName并捕获相关异常,然后在catch语句中还原为charset.defaultCharset或UTF-8等

如果返回正确的字符编码,是否可以验证“来自浏览器”中的内容。我的浏览器显示的内容正确。请通过设置进行检查,request.setHeaderContent-type,text/html;字符集=utf-8;。做了,但不起作用。。。我的浏览器显示页面编码为Cp1252 Windows-1252。不,这不起作用:我也在控制台中得到问号。请确保服务器返回UTF-8字符set@user2174738我编辑了我的答案以检查他[0]返回的NameValuePair数组,这可能会导致ArrayIndexOutOfBoundsException。然而,您的案例意味着您的响应实体中可能没有标题元素。在这种情况下,我想可能很难检测到响应的编码。您必须确定它是哪种编码,并像以前一样将其作为参数添加到InputStreamReader的初始化中。@user2174738。。。既然您在问题注释中说您的网页是用ANSI编码的,那么在InputStreamReader参数列表中使用它作为字符集编码。