【问题标题】:Qt doing get request to YouTube api , doesn't support utf8 charactersQt 向 YouTube api 发出请求,不支持 utf8 字符
【发布时间】:2012-02-19 14:41:02
【问题描述】:

我正在使用 Qt 进行简单的获取请求,但返回响应在 gibrich 中,这部分不是英语。 当我通过浏览器调用相同的请求时,一切都很好,我得到了正确的响应 我在这里缺少什么? 这是代码和 YouTube Api 调用。
API:
https://gdata.youtube.com/feeds/api/videos/cDholGGVc1M?v=2&alt=jsonc(或json)

这就是我在 Qt 中调用它的方式

 QUrl getUrl("https://gdata.youtube.com/feeds/api/videos/cDholGGVc1M?v=2&alt=jsonc");
     QNetworkRequest request;
     request.setRawHeader("User-Agent", USER_AGENT.toUtf8());
     request.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
     request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
     request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
     request.setRawHeader("Connection", "Keep-Alive");
     request.setUrl(getUrl);
     QEventLoop loop;
     //This tell the request only to cuntinue after all response is done
     QNetworkReply *reply = networkManager->get(request);
     connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
     loop.exec();       
     //return response 
     QByteArray data=reply->readAll();
     ApiResponse.append(data); // HERE IS ALL GIBRISH

【问题讨论】:

    标签: c++ qt https youtube-api


    【解决方案1】:

    这是一个编码问题。使用它来获取 Gibrich 的 UTF-8 表示:

    ApiResponse.append(QString::fromUtf8(data));
    

    而不是将数据直接放入您的 ApiResponse。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-24
      • 2020-04-09
      • 2017-11-20
      • 2013-08-08
      • 2014-02-12
      • 1970-01-01
      • 2021-09-11
      • 2019-11-23
      相关资源
      最近更新 更多