嗨,我试图从互联网上获取的数据中flutter,并且只要在所有的字符response.body都是英文一切都很好,但我得到这些结果与persian/arabic字符.
链接到页面我正在测试这个:http: //mobagym.com/media/mobagym-app-info/farsi.html (我也用其他网址测试了它,我的api得到了相同的结果)
这是我的代码(我也试过在a中显示结果Text Widget):
static Future<String> loadFarsi() async{
final response = await http.get("http://mobagym.com/media/mobagym-app-info/farsi.html",headers:{"charset":"utf-8","Accept-Charset":"utf-8"});
print(response.body);
return response.body;
}
Run Code Online (Sandbox Code Playgroud)
我试过删除标题但仍然没有运气.
final response = await http.get("http://mobagym.com/media/mobagym-app-info/farsi.html");
Run Code Online (Sandbox Code Playgroud)
这是我从android studio的日志:
Performing hot reload...
Reloaded 7 of 507 libraries in 1,333ms.
I/flutter (23060): <html>
I/flutter (23060): <head>
I/flutter (23060): <meta charset="utf-8"/>
I/flutter (23060): </head>
I/flutter (23060): <body>Ø³ÙØ§Ù Ø³ÙØ§Ù ÙØ±Ù اÛپسÙÙ</body>
I/flutter (23060): </html>
Run Code Online (Sandbox Code Playgroud)
这部分是错误的: Ø³ÙØ§ÙØ³ÙØ§ÙÙØ±ÙاÛپسÙÙ
虽然这样的东西是实际的文字: سلامسلاملرمایپسوم
在Android手机上测试Xperia z3 plus(Android 6.0)
使用Android studio:3.1.2
使用颤动:flutter_windows_v0.3.2-beta
结果显示文本小部件中的文本:
Ric*_*eap 26
Web服务器的Content-Type标头是Content-Type: text/html.请注意,不包括charset后缀.应该说Content-Type: text/html; charset=utf-8.package:http当要求解码为字符时,客户端会查找此字符集.如果缺少它,则默认为LATIN1(不是utf-8).
正如您所见,在Request上设置标头没有帮助,因为它是执行解码的Response.幸运的是,有一个简单的解决方案.只需将这些字节解码为String,就像这样.
Future<String> loadFarsi() async {
final response =
await http.get("http://mobagym.com/media/mobagym-app-info/farsi.html");
String body = utf8.decode(response.bodyBytes);
print(body);
return body;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4033 次 |
| 最近记录: |