質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

2697閲覧

itunesのapiでAppStoreのランキングを取得しようとしてget()としても、そのインスタンスの.bodyがnullになってしまう・・・

nk1

総合スコア42

Flutter

Flutterは、iOSとAndroidのアプリを同じコードで開発するためのフレームワークです。オープンソースで開発言語はDart。双方のプラットフォームにおける高度な実行パフォーマンスと開発効率を提供することを目的としています。

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Dart

Dartは、Googleによって開発されたJavaScriptの代替となることを目的に作られた、ウェブ向けのプログラミング言語である。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/09/01 12:50

編集2021/09/22 14:24

前提・実現したいこと

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の発見方法についてご存知の方が教えていただけると嬉しいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2021/09/01 13:39

そもそも {"feed":{}} という空の結果が返ってるように見えますね。
nk1

2021/09/02 03:29

返信ありがとうございます! URLが変わっていたのですね。新しいURLに対応させてみましたが、違うエラーがでるようになりました。 もし何か解決方法をご存知でしたら、教えていただけると嬉しいです。
hoshi-takanori

2021/09/02 03:30

URL の最後を /xml にすると XML が、/json にすると JSON が返るようなので、/json にしてみては。
nk1

2021/09/02 08:38

返信ありがとうございます! /jsonに変更したらjsonが返りました!ただ、それでも違うエラーが出ます。ご助言いただけると嬉しいです。
nk1

2021/09/02 09:51

壊れているんだったらどうにもならなさそうですね、、、 このサイトだと、今年の3月にすでに取得できなくなっていてますが、 https://discussions.apple.com/thread/252568207 Game-iによると今でも100位までとれていているっぽいことと https://twitter.com/game_idaa/status/1433285317025415168?s=12 「公式の」って言い方しているあたりから、他の方法でランキングを取得する方法があるっぽいですね。 https://twitter.com/game_idaa/status/1431867927037308929 調べてみます
guest

回答1

0

自己解決

hoshi-takanori

2021/09/02 18:00

たぶん ax.itunes.apple.com の方が古くて (http だし) 細かい指定ができなくて、rss.itunes.apple.com の方が新しいけど壊れてる気がしてきました…。
https://twitter.com/game_idaa/status/1431867927037308929

が正解でした。最近、壊れていたのがなおったっぽいので、問題は特になくなりました。ありがとうございました。

投稿2021/09/23 05:42

nk1

総合スコア42

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問