如何在android中获取utf-8API响应内容

如何在android中获取utf-8API响应内容,android,utf-8,character-encoding,Android,Utf 8,Character Encoding,编辑: 多亏了一些awnsers,我将我的方法编辑成这样,但仍然并没有摆脱?马克 public String makeHttpGetRequestUtf8(String urlStr) throws IOException { String result = ""; try { URL url = new URL(urlStr); //BufferedReader brIn = new BufferedReader(new InputStream

编辑: 多亏了一些awnsers,我将我的方法编辑成这样,但仍然并没有摆脱?马克

 public String makeHttpGetRequestUtf8(String urlStr) throws IOException {
    String result = "";
    try {
        URL url = new URL(urlStr);
        //BufferedReader brIn = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8")); //<--- Tried this first but didn't help.
        BufferedReader brIn = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)); //<-- This also didn't help :(

        String line = "";
        while ((line = brIn.readLine()) != null)
            result += line;
    }catch (Exception e){
        Log.e("HTT_GET", "Failed to make httpGetRequestUtf8: " + e);
    }
    String afterDecode = URLDecoder.decode(result, "UTF-8");
    return afterDecode;
}
这将返回带有问号的响应

httpGet 2:

 public String makeHttpGetRequestUtf8(String urlStr) throws IOException {
    String result = "";
    try {
        URL url = new URL(urlStr);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        InputStream isr = con.getInputStream();

        // convert inputstream to string
        if(isr != null)
            result = convertInputStreamToString(isr);
        else
            result = "Could not make httpGetResul success";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }
    return result;
}

// convert inputstream to String
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}
同样的故事

我认为我在这些编码问题上犯了一些原则性错误。
感谢您的提示。

要转换InputStream,您可以使用以下内容:

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
或者在Java7中,您可以这样做

BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
编辑:

您还可以尝试使用InputStreamReader(它将检测HTTP响应的编码):

如果不起作用,您可能需要在响应中设置编码,然后:

response.setContentType("text/html");    
response.setCharacterEncoding("UTF-8");

您可以尝试
UrlDecode()
。将UrlDecode添加到问题编辑中,仍然没有更改。谢谢,我两次都尝试了,但都没有成功。仍然有错误。我还将我更改的新方法添加到问题编辑中。我没有收到错误,但结果字符串中有问号。示例:“�阿尔德莱克特里特��d koos Infotehnologia kaabeldusega teostatud…“这些是什么”?字符应该是什么?以下是显示为的字符?标记:“ä”、“õ”、“ö”、“ü”
String responseBody = EntityUtils.toString(entity);
response.setContentType("text/html");    
response.setCharacterEncoding("UTF-8");