首页 > 解决方案 > Http请求为flutter web抛出XMLHttpRequest错误

问题描述

我正在使用 Flutter Web 创建一个 RSS 提要阅读器 Web 应用程序。此应用程序调用由不同提供商提供的公共 RSS 提要。

我正在使用 Http get 进行调用,它会引发XMLHttpRequest错误。

我无法得到任何回应。您能否让我知道如何解决此错误并获得回复?

例如:使用的 rss 提要是https://www.space.com/feeds/all

注意:在移动设备上进行相同测试时,它可以正常工作并提供响应。

谢谢


下面粘贴的是正在使用的代码

//Fetching the feed data for the given url
  Future<List<FeedItemDetails>> fetchFeedItemList(String category, String url) async {
    print("Calling URL : $url");
    http.Response response = await http
        .get(url)
        .timeout(Duration(seconds: Constants.NETWORK_TIMEOUT_SECONDS));

    if (response.statusCode == 200) {
      List<FeedItemDetails> feedItemList = List<FeedItemDetails>();

      switch (_getFeedType(response.body)) {
        case Constants.FEED_TYPE_RSS:
          {

            //Parsing the RSS feed from the response
            RssFeed rssFeed = RssFeed.parse(response.body);
            //Keeping the image of feed for adding in FeedItemDetails,
            String rssImageURL = rssFeed.image == null ? "" : rssFeed.image.url;
            feedItemList = rssFeed.items
                .map((item) => FeedItemDetails.parseRSSItem(item,category,rssImageURL))
                .toList();
          }
          break;
        case Constants.FEED_TYPE_ATOM:
          {
            //Parsing the RSS feed from the response
            AtomFeed atomFeed = AtomFeed.parse(response.body);
            //Keeping the logo of feed for adding in FeedItemDetails,
            String atomImageURL = atomFeed.logo == null ? "" : atomFeed.logo;
            feedItemList = atomFeed.items
                .map(
                    (item) => FeedItemDetails.parseAtomItem(item,category, atomImageURL))
                .toList();
          }
          break;
        default:
          {
            throw Exception(Constants.INVALID_FEED_DATA);
          }
      }
      return feedItemList;
    } else {
      // If service call was not successful, throw an error.
      throw Exception(Constants.INVALID_RESPONSE);
    }
  }

标签: httpfluttergetxmlhttprequestflutter-web

解决方案


推荐阅读