首页 > 解决方案 > Flutter:JSON响应不完整

问题描述

我正在尝试从 Flutter 中的 API 获取每周的股票数据。但是,响应很早就被切断了。我错过了什么?

static Future<List<StockWeek>> fetchAll() async {
    // creates: https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=MSFT&apikey=demo
    var uri = Endpoint.uri('', queryParameters: { "function" : "TIME_SERIES_WEEKLY", "symbol" : "MSFT", "apikey" : "demo" });

    final resp = await http.get(uri.toString());

    if (resp.statusCode != 200) {
      throw (resp.body);
    }

    print(resp.body.toString());

    List<StockWeek> list = new List<StockWeek>();

    // parse JSON

    return list;
  }

我在打印声明时得到的是:

I/flutter (19224): {
I/flutter (19224):     "Meta Data": {
I/flutter (19224):         "1. Information": "Weekly Prices (open, high, low, close) and Volumes",
I/flutter (19224):         "2. Symbol": "MSFT",
I/flutter (19224):         "3. Last Refreshed": "2018-12-03",
I/flutter (19224):         "4. Time Zone": "US/Eastern"
I/flutter (19224):     },
I/flutter (19224):     "Weekly Time Series": {
I/flutter (19224):         "2018-12-03": {
I/flutter (19224):             "1. open": "113.0000",
I/flutter (19224):             "2. high": "113.4200",
I/flutter (19224):             "3. low": "110.7300",
I/flutter (19224):             "4. close": "112.0900",
I/flutter (19224):             "5. volume": "34275048"
I/flutter (19224):         },
I/flutter (19224):         "2018-11-30": {
I/flutter (19224):             "1. open": "104.7900",
I/flutter (19224):             "2. high": "111.3300",
I/flutter (19224):             "3. low": "104.5800",
I/flutter (19224):             "4. close": "110.8900",
I/flutter (19224):             "5. volume": "170037931"
I/flutter (19224):         },
I/flutter (19224):         "2018-11-23": {
I/flutter (19224):             "1. open": "108.2700",
I/flutter (19224):             "2. high": "108.5600",
I/flutter (19224):             "3. low": "99.3528",
I/flutter (19224):             "4. close": "103.0700",
I/flutter (19224):             "5. volume": "150780076"
I/flutter (19224):         },
I/flutter (19224):         "2018-11-16": {
I/flutter (19224):             "1. open": "109.4200",
I/flutter (19224):             "2. high": "109.9600",
I/flutter (19224):             "3. low": "103.9100",
I/flutter (19224):             "4. 

它一直停在这一点上。与回复的外观相反:

https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=MSFT&apikey=demo

标签: jsonflutter

解决方案


print()当使用很多行时,这只是一个日志记录问题。按照这里的推荐使用debugPrint()Flutter 的foundation库。

import 'package:flutter/foundation.dart'; 

static Future<List<StockWeek>> fetchAll() async {
    // creates: https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=MSFT&apikey=demo
    var uri = Endpoint.uri('', queryParameters: { "function" : "TIME_SERIES_WEEKLY", "symbol" : "MSFT", "apikey" : "demo" });

    final resp = await http.get(uri.toString());

    if (resp.statusCode != 200) {
      throw (resp.body);
    }

    debugPrint(resp.body.toString());

    List<StockWeek> list = new List<StockWeek>();

    // parse JSON

    return list;   
}

推荐阅读