Flutter - 使用 API 键
Flutter - using an API key
我正在制作一个从 public v1 Api but support for this will soon be dropped, meaning that I'll have to migrate the the more powerful professional v1 Api.
中获取加密货币 JSON 数据的应用程序
唯一的问题是,我不知道如何实现解析 JSON 数据时所需的新 Api 键的使用。
我正在使用此 git repo 的经过大量修改的版本来编写应用程序,但所有基本功能都基于此处。
我所需要的只是关于我需要添加到此文件以显示新的专业 v1 Api 的指导,欢迎任何意见或想法。谢谢
这是 crypto_data_prod.dart
文件,我必须在其中更改我的代码以使用密钥。
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:fluttercrypto/data/crypto_data.dart';
class ProdCryptoRepository implements CryptoRepository {
String cryptoUrl = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
@override
Future<List<Crypto>> fetchCurrencies() async {
// TODO: implement fetchCurrencies
http.Response response = await http.get(cryptoUrl);
final List responseBody = JSON.decode(response.body);
final statusCode = response.statusCode;
if (statusCode != 200 || responseBody == null) {
throw new FetchDataException(
"An error ocurred : [Status Code : $statusCode]");
}
return responseBody.map((c) => new Crypto.fromMap(c)).toList();
}
}
尝试将 http.Response response = await http.get(cryptoUrl);
更改为
http.Response response = await http.get(cryptoUrl,
headers: {"X-CMC_PRO_API_KEY": "cab79c7b-52e9-4e4b-94fc-b0f32da14799"});
有关更多信息,请查看 this link。
我正在制作一个从 public v1 Api but support for this will soon be dropped, meaning that I'll have to migrate the the more powerful professional v1 Api.
中获取加密货币 JSON 数据的应用程序唯一的问题是,我不知道如何实现解析 JSON 数据时所需的新 Api 键的使用。
我正在使用此 git repo 的经过大量修改的版本来编写应用程序,但所有基本功能都基于此处。
我所需要的只是关于我需要添加到此文件以显示新的专业 v1 Api 的指导,欢迎任何意见或想法。谢谢
这是 crypto_data_prod.dart
文件,我必须在其中更改我的代码以使用密钥。
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:fluttercrypto/data/crypto_data.dart';
class ProdCryptoRepository implements CryptoRepository {
String cryptoUrl = "https://api.coinmarketcap.com/v1/ticker/?limit=50";
@override
Future<List<Crypto>> fetchCurrencies() async {
// TODO: implement fetchCurrencies
http.Response response = await http.get(cryptoUrl);
final List responseBody = JSON.decode(response.body);
final statusCode = response.statusCode;
if (statusCode != 200 || responseBody == null) {
throw new FetchDataException(
"An error ocurred : [Status Code : $statusCode]");
}
return responseBody.map((c) => new Crypto.fromMap(c)).toList();
}
}
尝试将 http.Response response = await http.get(cryptoUrl);
更改为
http.Response response = await http.get(cryptoUrl,
headers: {"X-CMC_PRO_API_KEY": "cab79c7b-52e9-4e4b-94fc-b0f32da14799"});
有关更多信息,请查看 this link。