来自 API 的 Android Studio 数据

Posted

技术标签:

【中文标题】来自 API 的 Android Studio 数据【英文标题】:Android Studio data from API 【发布时间】:2022-01-18 01:03:24 【问题描述】:

基本上我使用 API,我可以在其中获取车牌并显示车辆信息。我设法让一切正常,但似乎无法弄清楚如何显示所有信息。

Future<Album> fetchAlbum() async 
  final response = await http
      .get(Uri.parse('https://v1.motorapi.dk/vehicles/CZ33849'),
       headers: "X-AUTH-TOKEN": "rfrzsucnc7eo3m5hcmq6ljdzda1lz793",
        "Content-Type": "application/json",
        "Accept": "application/json",
      );

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


//headers: "X-AUTH-TOKEN": "rfrzsucnc7eo3m5hcmq6ljdzda1lz793"

class Album 
  final String? registration_number;
  final String? status;
  final String? type;
  final String? use;
  final String? first_registration;
  final String? vin;
  final int? doors;
  final String? make;
  final String? model;
  final String? variant;
  final String? model_type;
  final String? color;
  final String? chasis_type;
  final int? engine_power;
  final String? fuel_type;
  final String? RegistreringssynToldsyn;
  final String? date;
  final String? result;

  Album(
    this.registration_number,
    this.status,
    this.type,
    this.use,
    this.first_registration,
    this.vin,
    this.doors,
    this.make,
    this.model,
    this.variant,
    this.model_type,
    this.color,
    this.chasis_type,
    this.engine_power,
    this.fuel_type,
    this.RegistreringssynToldsyn,
    this.date,
    this.result,
  );

  factory Album.fromJson(Map<String, dynamic> json) 
    return Album(
      registration_number: json['registration_number'],
      status: json['status'],
      type: json['type'],
      use: json['use'],
      first_registration: json['first_registration'],
      vin: json['vin'],
      doors: json['doors'],
      make: json['make'],
      model: json['model'],
      variant: json['variant'],
      model_type: json['model_type'],
      color: json['color'],
      chasis_type: json['chasis_type'],
      engine_power: json['engine_power'],
      fuel_type: json['fuel_type'],
      RegistreringssynToldsyn: json['RegistreringssynToldsyn'],
      date: json['date'],
      result: json['result'],
    );
  


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

class MyApp extends StatefulWidget 
  const MyApp(Key? key) : super(key: key);

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


class _MyAppState extends State<MyApp> 
  late 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: const Text('Fetch Data Example'),
        ),
        body: Center(
          child: FutureBuilder<Album>(
            future: futureAlbum,
            builder: (context, snapshot) 
              if (snapshot.hasData) 
                return Text("$snapshot.data!.use");


               else if (snapshot.hasError) 
                return Text('$snapshot.error');
              

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

当我编辑这一行时:return Text("$snapshot.data!.use");,我可以将 use 替换为 Album 类中的其他变量,但我似乎无法弄清楚如何一次显示所有变量。

【问题讨论】:

【参考方案1】:

你想如何显示?这取决于您的用户界面。如果你想把它全部放在一个 Text 小部件中,你可以这样做

class User 
  String name;
  String address;

  toString() 
    return "name: " + name + ", address: " + address;
  

或者您可以使用列/行小部件来显示每个文本小部件。

【讨论】:

如何添加它以返回 Text("$snapshot.data!.use"); ? @mhmn 替换为 --- return Column( children: [ Text("$snapshot.data!.use"), Text("$snapshot.data!.make" ), Text("$snapshot.data!.model"), Text("$snapshot.data!.variant"), ], );

以上是关于来自 API 的 Android Studio 数据的主要内容,如果未能解决你的问题,请参考以下文章

来自 Google Drive Android Studio API 的图像被旋转

使用不推荐使用的 API 时,Android Studio 无法编译

使用 Google map api 在 android Studio 中解析 Json

为啥我的 API 和 JSON 应用无法在 Android Studio 中运行

Android (Studio) FileNotFoundException 但文件在 SD 卡上

android studio 如何修改api版本