前提・実現したいこと
FlutterでAppStoreのアプリランキングを表示するアプリを考えております。
エミュレータで起動したところ、apiによってデータを取得できませんでした。
print関数をコード中に挟んでみて調べたところ
var url = Uri.parse("http・・・");
http.Response response = await http.get(url);
とデータを取得したものの、
data = json.decode(response.body);
でdataが空になっているのが原因のようです。(response.bodyがnullになっている)(urlに使われる初期値はcurrentCode = "us",currentType="top-free",currentGenre="all"に他の場所で設定してあります。)
dataにしっかり値が代入されるようにしたいです。
print関数でconsoleに出力されたもの
flutter: us flutter: https://rss.itunes.apple.com/api/v1/us/ios-apps/top-free/all/200/explicit.json flutter: Instance of 'Response' flutter: {"feed":{}} flutter: 200 flutter: {feed: {}} flutter: {} flutter: null
該当のソースコード(プログラムの一部を抜き出しました)
dart
1import 'package:flutter/material.dart'; 2import 'package:http/http.dart' as http; 3import 'dart:async'; 4import 'dart:convert'; 5import 'package:url_launcher/url_launcher.dart'; 6 7 8 List? rankingData; 9 10 String? currentCode; 11 String? currentType; 12 String? currentGenre; 13 14 **Future getData() async { 15 print(currentCode); 16 var url = Uri.parse( 17 "https://rss.itunes.apple.com/api/v1/$currentCode/ios-apps/$currentType/$currentGenre/200/explicit.json"); 18 print(url); 19 http.Response response = await http.get(url); 20 print(response); 21 print(response.body); 22 print(response.statusCode); 23 24 if (response.statusCode == 200) { 25 // ここの.bodyでdataがnullになってしまう。。。 26 data = json.decode(response.body); 27 print(data); 28 feedData = data["feed"]; 29 print(feedData); 30 31 setState(() { 32 rankingData = feedData["results"]; 33 print(rankingData); 34 }); 35 } else { 36 setState(() { 37 rankingData = []; 38 }); 39 } 40 }** 41 42・・・・・・・続く 43 44
試したこと
グーグルで 「response body null flutter」と検索して、下記のサイトに辿り着きましたが、そこでも解決されていないようです。
https://stackoverflow.com/questions/64942048/flutter-response-body-is-empty
追記
おっしゃるとおりでURLが変わっていたようなので、新しいURLで試してみました。
(初期値は currentCode = 'us';
currentType = 'topfree';
currentGenre = 'applications';に)
dart
1String? currentTypeAndGenre = "$currentType$currentGenre"; 2var url = Uri.parse( 3 "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/$currentTypeAndGenre/cc=$currentCode/limit=200/xml"); 4 print(url);
print(url)まではteminalに出力されましたが、その後の
http.Response response = await http.get(url);
print(response);
がうまくいかないようで
console
1flutter: us 2flutter: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/cc=us/limit=200/xml 3======== Exception caught by widgets library ======================================================= 4The following _TypeError was thrown building RankingPage(dirty, state: _RankingPageState#aa6a8): 5type 'Null' is not a subtype of type 'String' 6 7The relevant error-causing widget was: 8 RankingPage file:///Users/nt/StudioProjects/app_ranking/lib/main.dart:18:13 9When the exception was thrown, this was the stack: 10#0 _RankingPageState.build (package:app_ranking/ranking_page.dart:177:44) 11#1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27) 12#2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) 13#3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) 14#4 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5) 15... 16==================================================================================================== 17
とエラーがでるようになりました、、、
取得したurlのページを開くと頭に
This XML file does not appear to have any style information associated with it. The document tree is shown below.
と書いており、これが原因なのでしょうか?
なにかご存じの方がいたら教えていただけると嬉しいです。
追記②
おっしゃるとおり、urlの最後をxmlからjsonに変えたところ、
feedData = data["feed"];
print(feedData);
までデータをとることができました。しかし、もとのコードとデータの取得先が違うのか"results"という項目がないためか、
rankingData = feedData["results"];
はうまくいきませんでした。
そこで、取得したurl
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/cc=us/limit=200/json
のページを眺めたところ、"entry"が、"results"に当たりそうなので、その部分を変更し、rankingDataに"entry"の値を代入することができました。しかし、その後がうまくいきません。
エラー内容
console
1flutter: us 2flutter: http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/cc=us/limit=200/json 3 4======== Exception caught by widgets library ======================================================= 5The following _TypeError was thrown building RankingPage(dirty, state: _RankingPageState#5f572): 6type 'Null' is not a subtype of type 'String' 7 8The relevant error-causing widget was: 9 RankingPage file:///Users/nt/StudioProjects/app_ranking/lib/main.dart:18:13 10When the exception was thrown, this was the stack: 11#0 _RankingPageState.build (package:app_ranking/ranking_page.dart:177:44) 12#1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27) 13#2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) 14#3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) 15#4 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5) 16... 17==================================================================================================== 18flutter: Instance of 'Response' 19flutter: {"feed":{"author":{"name":{"label":"iTunes Store"}, "uri":{"label":"http://www.apple.com/itunes/"}}, "entry":[ 20{"im:name":{"label":"TikTok"}, "im:image":[ 21{"label":"https://is2-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/53x53bb.png", "attributes":{"height":"53"}}, 22{"label":"https://is1-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/75x75bb.png", "attributes":{"height":"75"}}, 23{"label":"https://is3-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/100x100bb.png", "attributes":{"height":"100"}}], "summary":{"label":"TikTok is THE destination for mobile videos. On TikTok, short-form video<…> 24flutter: 200 25flutter: {feed: {author: {name: {label: iTunes Store}, uri: {label: http://www.apple.com/itunes/}}, entry: [{im:name: {label: TikTok}, im:image: [{label: https://is2-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/53x53bb.png, attributes: {height: 53}}, {label: https://is1-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/75x75bb.png, attributes: {height: 75}}, {label: https://is3-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/100x100bb.png, attributes: {height: 100}}], summary: {label: TikTok is THE destination for mobile videos. On TikTok, short-form videos are exciting, spontaneous, and genuine. Wh<…> 26flutter: {author: {name: {label: iTunes Store}, uri: {label: http://www.apple.com/itunes/}}, entry: [{im:name: {label: TikTok}, im:image: [{label: https://is2-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/53x53bb.png, attributes: {height: 53}}, {label: https://is1-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/75x75bb.png, attributes: {height: 75}}, {label: https://is3-ssl.mzstatic.com/image/thumb/Purple125/v4/d5/f8/3e/d5f83ec1-8903-ba99-2605-ddc4ce0ecd51/AppIcon_TikTok-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/100x100bb.png, attributes: {height: 100}}], summary: {label: TikTok is THE destination for mobile videos. On TikTok, short-form videos are exciting, spontaneous, and genuine. Whether y<…> 27flutter: null 28 29======== Exception caught by widgets library ======================================================= 30The following _TypeError was thrown building RankingPage(dirty, state: _RankingPageState#5f572): 31type 'Null' is not a subtype of type 'String' 32 33The relevant error-causing widget was: 34 RankingPage file:///Users/nt/StudioProjects/app_ranking/lib/main.dart:18:13 35When the exception was thrown, this was the stack: 36#0 _RankingPageState.build (package:app_ranking/ranking_page.dart:177:44) 37#1 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27) 38#2 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15) 39#3 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11) 40#4 Element.rebuild (package:flutter/src/widgets/framework.dart:4267:5) 41... 42==================================================================================================== 43Lost connection to device.
また、urlにcc="国名"を入れることで、各国のランキングがとれるようになるかと思いきや、どれも変わりませんでした。
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/limit=200/json
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/cc=us/limit=200/json
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topfreeapplications/cc=jp/limit=200/json
データの取得方法と、各国のランキングのurlの発見方法についてご存知の方が教えていただけると嬉しいです。
回答1件
あなたの回答
tips
プレビュー