首页 > 解决方案 > Flutter http response.body utf8 编码错误

问题描述

我开始学习 Flutter,并通过制作自己的manga阅读应用程序来做到这一点,其中我从我最常使用的网站上抓取所有数据。

我的问题是,mangas由于这个错误,我读到的只有一个我无法抓取数据:

FormatException (FormatException: Bad UTF-8 encoding 0x22 (at offset 369))

我的刮板代码

    Future<Manga> getMangaInfo(source) async{
    final response =  await _client.get(source);
    var manga;
    print(response.body);//error occurs here
    final document = parse(response.body);

    final mangaInfo = document.getElementsByClassName('tamanho-bloco-perfil');
    for(Element infos in mangaInfo){
      final infoCont = infos.getElementsByClassName('row');
      //get titulo
      Element tituloCont = infoCont[0];
      final tituloH = tituloCont.getElementsByTagName('h2');
      Element tituloCont2 = tituloH[0];
      String titulo = '['+tituloCont2.text+']';
      //print(titulo);

      //get capa

      Element capaCont = infoCont[2];
      final capaImg = capaCont.getElementsByTagName('img');
      Element capaCont2 = capaImg[0];
      final capaUrl = capaCont2.attributes['src'];

      //get caprecente
      final capsPorNumero = document.getElementsByClassName('row lancamento-linha');
      final caps = capsPorNumero[0].getElementsByTagName('a');
      Element info = caps[0];
      final numero = info.text.split(' ')[1];
      final capRecenteUrl = info.attributes['href'];

      manga = Manga(null,source,titulo,capaUrl,numero,capRecenteUrl);


    }
    return manga;

  }

response.body给出错误的那个

我也尝试过使用response.bodyBytes和解码,但仍然无法修复

这是该页面的链接: https ://unionleitor.top/perfil-manga/kimetsu-no-yaiba

我猜问题是html头上以下元标记上的�字符

<meta name="description" content="Kimetsu no Yaiba - Novo mangá sobrenatural da Shonen Jump. O mangá conta a história de Tanjiro, o filho mais velho de uma família que �">

我还没有找到解决方案,也许我只是看错了地方。谁能帮我解决这个问题?
谢谢!

标签: htmlhttpflutterutf-8

解决方案


我只是做:

utf8.decode(response.bodyBytes);

即使你得到一个 JSON

jsonDecode(utf8.decode(response.bodyBytes))

推荐阅读