【问题标题】:unhandled Exception: SocketException: Failed host lookup: URL (OS Error: No address associated with hostname, errno = 7)未处理的异常:SocketException:主机查找失败:URL(操作系统错误:没有与主机名关联的地址,errno = 7)
【发布时间】:2021-08-31 20:39:26
【问题描述】:

编辑:问题现已解决


我在here 上尝试了解决方案,但对我来说不起作用。

案例

  1. 我已提供互联网权限
  2. 我在实际设备上运行它
  3. 我的设备上有一个有效的互联网连接

基本上,我在 python 中构建了一个 API,并将其部署在 vercel 上。现在我正在尝试使用颤振从我的 API 中获取详细信息。链接是https://dailyhunt.vandit.cf/dailyhunt?category=technology

它在web 上运行良好,但在实际设备上无法运行

我的代码太长并且被分隔在多个文件中,所以我可以提供这个:

import 'package:news_app/models/article_model.dart';
import 'package:http/http.dart' as http;
import 'package:news_app/constants.dart';
import 'dart:convert';

class News {
  List<ArticleModel> news = [];

  Future getNews() async {
    http.Client client = http.Client();
    http.Response response = await client.get(Uri.parse(kDailyhuntEndpoint));

    if (response.statusCode == 200) {
      var jsonData = jsonDecode(response.body);

      if (jsonData['success'] == true) {
        jsonData['data'].forEach((element) {
          if (element['imageUrl'] != "" && element['content'] != "") {
            List<String> raw = element['PublishedTime'].split(" ");
            String date = raw[0];
            String time = raw[1];
            ArticleModel articleModel = ArticleModel(
              publishedDate: date,
              publishedTime: time,
              image: element['imageUrl'].toString(),
              content: element['content'].toString(),
              fullArticle: element['publisherStory'].toString(),
              views: element['viewCount'].toString(),
              title: element['title'].toString(),
            );
            news.add(articleModel);
          }
        });
      } else {
        print('ERROR');
      }
    }
  }
}

【问题讨论】:

  • 我也面临同样的问题!
  • 你在模拟器上测试吗?
  • @OMiShah 在实际设备上

标签: flutter http dart flutter-http


【解决方案1】:

它正在响应。我已经在真实设备上测试过它工作正常

    Future getNews() async {
    http.Response response = await http
        .get("https://dailyhunt.vandit.cf/dailyhunt?category=technology");

    if (response.statusCode == 200) {
      var jsonData = jsonDecode(response.body);
      print(jsonData);

      if (jsonData['success'] == true) {}
    } else {
      print('ERROR');
    }
  }

【讨论】:

  • 如果我打印它,它也会给我一个响应,但是当我运行应用程序时,它会显示上述错误
  • 运行应用程序?喜欢执行一些任务?我无法清楚地理解。
  • "dailyhunt.vandit.cf/dailyhunt?category=technology" 这个网址现在不起作用
  • 我的问题已解决,API 运行良好 IDK id 在您发表评论时它已关闭
猜你喜欢
  • 1970-01-01
  • 2021-04-19
  • 2020-11-29
  • 2019-09-28
  • 2023-03-26
  • 2019-07-12
  • 2021-05-09
  • 1970-01-01
  • 2020-05-10
相关资源
最近更新 更多