我想在我的 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);
关于java - Android HTTP 请求编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18084475/