【问题标题】:Google book api in postman working fine But in flutter giving status code 404邮递员中的 Google book api 工作正常但在颤振中给出状态码 404
【发布时间】:2021-06-18 12:18:29
【问题描述】:

当我在邮递员中运行 google book API 时,它工作正常并给出状态码 200。但在颤振中,它给出了以下错误。并给出 404 状态码。 我已经生成了 API 密钥,还启用了 Google Book API。 所以如果有人知道我做错了什么,请帮助我。

TestLayout.dart

   class TestLayout extends StatefulWidget {
      static String id = "Test_Layout";
    
      @override
      _TestLayoutState createState() => _TestLayoutState();
    }
    
    class _TestLayoutState extends State<TestLayout> {
      Future<void> getData() async {
        String ApiKey = "..........";
    
        final response = await http.get(
            Uri.https("books.googleapis.com",
                "books/v1/volumes?q=flowers+inauthor:keyes&key=$ApiKey"),
            headers: {"Content-Type": "application/json"});
    
        var jsonBody = jsonDecode(response.body);
    
        //book b1=book.fromJson(jsonBody);
    
        print(response.statusCode);
        print(b1.id);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Flutter"),
          ),
          body: Column(
            children: [
              TextButton(
                  onPressed: () async {
                    await getData();
                  },
                  child: Text("Submit")),
            ],
          ),
        );
      }
    }
   

Book.dart:

class book {

  final int id;

   book({this.id});

   factory book.fromJson(Map<String,dynamic> json)
   {
     return book(
       id: json["totalItems"]
     );
   }
}
   

错误:

    E/flutter (27854): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: 
    FormatException: Unexpected character (at character 1)
    E/flutter (27854): <!DOCTYPE html>
    E/flutter (27854): ^
    E/flutter (27854): 
    E/flutter (27854): #0      _ChunkedJsonParser.fail (dart:convert- 
    patch/convert_patch.dart:1404:5)
    E/flutter (27854): #1      _ChunkedJsonParser.parseNumber (dart:convert- 
     patch/convert_patch.dart:1271:9)
    E/flutter (27854): #2      _ChunkedJsonParser.parse (dart:convert- 
    patch/convert_patch.dart:936:22)
    E/flutter (27854): #3      _parseJson (dart:convert-patch/convert_patch.dart:40:10)
    E/flutter (27854): #4      JsonDecoder.convert (dart:convert/json.dart:506:36)
    E/flutter (27854): #5      JsonCodec.decode (dart:convert/json.dart:157:41)
    E/flutter (27854): #6      jsonDecode (dart:convert/json.dart:96:10)
    E/flutter (27854): #7      _TestLayoutState.getData 
    (package:freebookshare/TestFile/TestLayout.dart:22:20)
    E/flutter (27854): <asynchronous suspension>
    E/flutter (27854): #8      _TestLayoutState.build.<anonymous closure> 
    (package:freebookshare/TestFile/TestLayout.dart:40:17)
    E/flutter (27854): <asynchronous suspension>
    E/flutter (27854): 

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    我使用了 Uri.parse() 方法,它成功了。

    String url="https://books.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=$ApiKey";

    final response = await http.get(Uri.parse(url),
        headers: {"Content-Type": "application/json"});
    

    【讨论】:

      【解决方案2】:

      这是一个老问题,我以前也遇到过。在进行了一些研究之后,我通过删除 API 调用中的 Uri 构造函数获得了必要的输出。尝试删除 Uri Constructor 并添加最终响应的代码如下

          final response = await http.get("https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=$apiKey",
              headers: {"Content-Type": "application/json"});
      

      之后TestLayout的代码如下

          class TestLayout extends StatefulWidget {
        static String id = "Test_Layout";
      
        @override
        _TestLayoutState createState() => _TestLayoutState();
      }
      
      class _TestLayoutState extends State<TestLayout> {
        Future<void> getData() async {
          String apiKey = "..........";
      
          final response = await http.get("https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes&key=$apiKey",
              headers: {"Content-Type": "application/json"});
      
          var jsonBody = jsonDecode(response.body);
          
          book b1=book.fromJson(jsonBody);
          
          print(response.statusCode);
          print(b1.id);
        }
      
        @override
        Widget build(BuildContext context) {
          return Scaffold(
            appBar: AppBar(
              title: Text("Flutter"),
            ),
            body: Column(
              children: [
                TextButton(
                    onPressed: () async {
                      await getData();
                    },
                    child: Text("Submit")),
              ],
            ),
          );
        }
      }
      

      希望这对你有用

      【讨论】:

      • 我删除了 Uri 构造函数,但它给出了一个错误,例如“参数类型 'String' 不能分配给参数类型 Uri”
      • 您是否正确更改了最终回复行,如我输入的行中所述?因为它对我来说完全没问题。 (我刚刚创建了一个虚拟项目,结果是一样的。状态码是 200,甚至我能够打印 JSON 对象)
      • 我认为新版本的dart get方法不接受String作为参数。
      猜你喜欢
      • 2020-05-19
      • 1970-01-01
      • 2020-08-27
      • 2017-08-16
      • 2020-05-25
      • 1970-01-01
      • 2021-10-08
      • 2021-12-27
      • 1970-01-01
      相关资源
      最近更新 更多