解码uri.https时的错误,“未经手的异常:formatexception:无效的字符(在字符27)”

发布于 2025-01-21 13:00:15 字数 1896 浏览 0 评论 0原文

我正在尝试构建天气应用程序。我已经搜索了天气API,因为我需要每小时和每周进行天气预报。我找到了这个网站 https://www.visualcrossing.com/ 。我想像这样解码HTTP链接(以下在代码中):

class DataServiceOneCall {
  Future<WeatherResponseOnecall> getWeatherOneCall(String city) async {
    //https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Olszowice?unitGroup=metric&key=QVDLJFCCQRM5296M7J975FP2W&contentType=json

    final queryParameters = {
      'q': city,
      'unitGroup': 'metric',
      'key': 'QVDLJFCCQRM5296M7J975FP2W',
      'contentType': 'json',
    };

    final uri = Uri.https(
        'weather.visualcrossing.com/VisualCrossingWebServices',
        '/rest/services/timeline,',
        queryParameters);

    final response_onecall = await http.get(uri);

    print(response_onecall.body);

    final json = jsonDecode(response_onecall.body);
    return WeatherResponseOnecall.fromJson(json);
  }
}

我有这样的错误。

E/flutter (11332): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Invalid character (at character 27)
E/flutter (11332): weather.visualcrossing.com/VisualCrossingWebServices
E/flutter (11332):                           ^
E/flutter (11332):
E/flutter (11332): #0      _Uri._fail (dart:core/uri.dart:1754:5)
E/flutter (11332): #1      _Uri._normalizeRegName (dart:core/uri.dart:2252:9)
E/flutter (11332): #2      _Uri._makeHost (dart:core/uri.dart:2113:12)
E/flutter (11332): #3      new _Uri (dart:core/uri.dart:1640:12)
E/flutter (11332): #4      _Uri._makeHttpUri (dart:core/uri.dart:1813:12)
E/flutter (11332): #5      new _Uri.https (dart:core/uri.dart:1678:12)
E/flutter (11332): #6      DataServiceOneCall.getWeatherOneCall package:weather_app/data_service/data_service_onecall.dart:18

感谢您的帮助。

I'm trying to built weather app. I've searched weather API's, because I need make hourly and weekly weather forecast. I found this website https://www.visualcrossing.com/. I want to decode HTTP link (below in code) like this:

class DataServiceOneCall {
  Future<WeatherResponseOnecall> getWeatherOneCall(String city) async {
    //https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Olszowice?unitGroup=metric&key=QVDLJFCCQRM5296M7J975FP2W&contentType=json

    final queryParameters = {
      'q': city,
      'unitGroup': 'metric',
      'key': 'QVDLJFCCQRM5296M7J975FP2W',
      'contentType': 'json',
    };

    final uri = Uri.https(
        'weather.visualcrossing.com/VisualCrossingWebServices',
        '/rest/services/timeline,',
        queryParameters);

    final response_onecall = await http.get(uri);

    print(response_onecall.body);

    final json = jsonDecode(response_onecall.body);
    return WeatherResponseOnecall.fromJson(json);
  }
}

And I've got error thrown like this.

E/flutter (11332): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FormatException: Invalid character (at character 27)
E/flutter (11332): weather.visualcrossing.com/VisualCrossingWebServices
E/flutter (11332):                           ^
E/flutter (11332):
E/flutter (11332): #0      _Uri._fail (dart:core/uri.dart:1754:5)
E/flutter (11332): #1      _Uri._normalizeRegName (dart:core/uri.dart:2252:9)
E/flutter (11332): #2      _Uri._makeHost (dart:core/uri.dart:2113:12)
E/flutter (11332): #3      new _Uri (dart:core/uri.dart:1640:12)
E/flutter (11332): #4      _Uri._makeHttpUri (dart:core/uri.dart:1813:12)
E/flutter (11332): #5      new _Uri.https (dart:core/uri.dart:1678:12)
E/flutter (11332): #6      DataServiceOneCall.getWeatherOneCall package:weather_app/data_service/data_service_onecall.dart:18

Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金橙橙 2025-01-28 13:00:15

我像这样重写了此代码,并且可以正常工作。

final uri = Uri.https(
        'weather.visualcrossing.com',
        '/VisualCrossingWebServices/rest/services/timeline/${city}?',
        queryParameters);

I rewrite this code like this and it works.

final uri = Uri.https(
        'weather.visualcrossing.com',
        '/VisualCrossingWebServices/rest/services/timeline/${city}?',
        queryParameters);
晌融 2025-01-28 13:00:15

v斜线后会很小。

final uri = Uri.https(
    'weather.visualcrossing.com/visualCrossingWebServices',
    '/rest/services/timeline,',

V after slash would be small.

final uri = Uri.https(
    'weather.visualcrossing.com/visualCrossingWebServices',
    '/rest/services/timeline,',
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文