Исправление этой ошибки при формировании исключений при извлечении данных из API во Flutter - PullRequest
0 голосов
/ 22 февраля 2020

Так что это мой полный код. Он относительно короткий, потому что я просто пытаюсь получить заголовок видео и идентификатор видео из invidiou. sh api: 'https://invidiou.sh/api/v1/search?q=Corridor+Crew'

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<Album> fetchAlbum() async {
  final response =
      await http.get('https://invidiou.sh/api/v1/search?q=Corridor+Crew');

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response, then parse the JSON.
    return Album.fromJson(json.decode(response.body));
  } else {
    // If the server did not return a 200 OK response, then throw an exception.
    throw Exception('Failed to load album');
  }
}

class Album {
  final String videoId;
  final String title;

  Album({this.videoId, this.title});

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      videoId: json['videoId'],
      title: json['title'],
    );
  }
}

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Future<Album> futureAlbum;

  @override
  void initState() {
    super.initState();
    futureAlbum = fetchAlbum();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fetch Data Example'),
        ),
        body: Center(
          child: FutureBuilder<Album>(
            future: futureAlbum,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data.title);
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              }

              // By default, show a loading spinner.
              return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
}

Следующее исключение

Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE HTML>
^
)

встречается здесь в response.body, и я не знаю, что с этим делать:

Future<Album> fetchAlbum() async {
  final response =
      await http.get('https://invidiou.sh/api/v1/search?q=Corridor+Crew');

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response, then parse the JSON.
    return Album.fromJson(json.decode(response.body)); <---- HERE!
  } else {
    // If the server did not return a 200 OK response, then throw an exception.
    throw Exception('Failed to load album');
  }
}

Ответы [ 2 ]

1 голос
/ 22 февраля 2020

если вы распечатаете тело ответа API, вы можете найти его:

 <!DOCTYPE HTML>
  <html lang="en-US">

  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="UTF-8">
    <title>Challenge Failed! vDDoS Security</title>
  </head>

  <body>
    <table width="100%" height="100%" cellpadding="30">
      <tr>
        <td align="center" valign="center">

          <h1 data-translate="challenge_headline" style="background-color:                white; border: 0px; color: #404040; font-family: &quot;Open Sans&quot;, Helvetica,      Arial, sans-serif; font-size: 5vw; font-stretch: inherit; font-variant-numeric: inherit; font-weight: 300; line-height: 1.2; margin: 0px; padding: 0px; vertical-     align: baseline;">
            vDDoS Security</h1>

          <noscript>
            <h1 style="text-align:center;color:red;">
              <strong>Please turn JavaScript on and reload the page.</strong>
            </h1>
          </noscript>

, поэтому этот API не будет работать без javascript из-за безопасности vDDOS, поэтому не работает с мобильного телефона.

0 голосов
/ 22 февраля 2020

На самом деле ваш ответ возвращает список, а не карту!

Попробуйте это! (Но я не уверен в этом <!DOCTYPE..... !)

Future<List<Album>> fetchAlbum() async {

  List<Album> albums =[];
  final response =
      await http.get('https://invidiou.sh/api/v1/search?q=Corridor+Crew');

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response, then parse the JSON.
    List<Map<String,dynamic>> list = List.from(response.body);
    for(Map<String,dynamic> m in list)
         albums.add(Album.fromJson(m));

    return albums;
  } else {
    // If the server did not return a 200 OK response, then throw an exception.
    throw Exception('Failed to load album');
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...