我正在使用 dio ,我正在传递表单数据并获得响应,但无法将数据存储到对象。请帮我 当我打印response.data时,它给出了响应,但是我没有得到如何将响应存储到对象中。
class MainActivityGrid extends StatefulWidget {
@override
_MainActivityGridState createState() => _MainActivityGridState();
}
class _MainActivityGridState extends State<MainActivityGrid> {
// List<ImageModel>images= [];
@override
Widget build(BuildContext context) {
return MaterialApp(
home: new Scaffold(
appBar: AppBar(
title: new Text("IPTV"),
),
body: new Center(
child: new RaisedButton(
child: new Text("Button"), onPressed: fetchImage),
),
),
);
}
Future<String> fetchImage() async {
String url = "http://xxxxxx:xxx/onnet_api/mediaList.php";
Response response;
Dio dio = new Dio();
FormData formData = new FormData.from(
{"publisherid": 102, "tag": "media", "subtag": "list"});
// counter ++;
response = await dio.post(url, data: formData);
// dio.interceptors.add(LogInterceptor(responseBody: fa));
//int status = response.data["status"]["data"];
//Map responseBody = response.data;
//loginResponse = new LoginResponse.fromJson(json.decode(response.data));
print(response.data["data"][0]);
//ImageModel imageModel = new ImageModel.fromJson(json.decode(response.data[]));
//print('response media: $status');
/* setState(() {
//images.add(imageModel);
});*/
}
}
我对我的API的JSON响应如下所示:请尝试帮助我。
{
"status": 1,
"data": [
{
"id": "0_314t2peg",
"name": "Countdown",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_314t2peg/version/100002",
"plays": 12,
"categories": "News>BBC,News>CNBC,News>PublicTV,News>TV9,Shows>Thriller,Shows>Regional",
"categoriesIds": "19,20,18,17,13,16",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_314t2peg/format/url/protocol/http",
"duration": 13,
"createdAt": "28/11/18 11:26:39",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_joyzmp2z",
"name": "voice 4k",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_joyzmp2z/version/100002",
"plays": 14,
"categories": "News>BBC,News>CNBC,News>PublicTV,News>TV9,Shows>Thriller,Shows>Regional",
"categoriesIds": "19,20,18,17,13,16",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_joyzmp2z/format/url/protocol/http",
"duration": 20,
"createdAt": "28/11/18 11:25:56",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_yndwskxv",
"name": "news",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_yndwskxv/version/100012",
"plays": 6,
"categories": "News>BBC,News>TV9,Shows>Regional",
"categoriesIds": "19,17,16",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_yndwskxv/format/url/protocol/http",
"duration": 17,
"createdAt": "28/11/18 11:24:12",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_jd7hul0b",
"name": "GB10Sec",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_jd7hul0b/version/100002",
"plays": 8,
"categories": "Shows>Thriller,Shows>Action,Shows>Sci-Fi",
"categoriesIds": "13,15,14",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_jd7hul0b/format/url/protocol/http",
"duration": 10,
"createdAt": "28/11/18 11:14:40",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_bz9qlkle",
"name": "Thugs Of Hindostan",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_bz9qlkle/version/100002",
"plays": 0,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_bz9qlkle/format/url/protocol/http",
"duration": 218,
"createdAt": "28/11/18 11:08:45",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_swm5unow",
"name": "Shivaay",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_swm5unow/version/100002",
"plays": 0,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_swm5unow/format/url/protocol/http",
"duration": 230,
"createdAt": "28/11/18 11:08:23",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_1n1qg0hq",
"name": "Rocky Handsome",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_1n1qg0hq/version/100012",
"plays": 9,
"categories": "Movies>Drama,Movies>Sci-Fi",
"categoriesIds": "22,24",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_1n1qg0hq/format/url/protocol/http",
"duration": 184,
"createdAt": "28/11/18 11:08:03",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_3vk4o4ie",
"name": "Genius",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_3vk4o4ie/version/100012",
"plays": 5,
"categories": "Movies>Drama,Movies>Sci-Fi",
"categoriesIds": "22,24",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_3vk4o4ie/format/url/protocol/http",
"duration": 200,
"createdAt": "28/11/18 11:07:46",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_jb8en5kn",
"name": "Commando",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_jb8en5kn/version/100012",
"plays": 2,
"categories": "Movies>Thriller,Movies>Drama,Movies>Sci-Fi",
"categoriesIds": "23,22,24",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_jb8en5kn/format/url/protocol/http",
"duration": 162,
"createdAt": "28/11/18 11:07:25",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_es5vzzca",
"name": "Action Jackson",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_es5vzzca/version/100002",
"plays": 0,
"categories": "Movies>Thriller,Movies>Drama,Movies>Sci-Fi",
"categoriesIds": "23,22,24",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_es5vzzca/format/url/protocol/http",
"duration": 175,
"createdAt": "28/11/18 11:06:13",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_2tfugvsm",
"name": "jellyfish-25-mbps-hd-hevc",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_2tfugvsm/version/100002",
"plays": 0,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_2tfugvsm/format/url/protocol/http",
"duration": 30,
"createdAt": "22/11/18 09:05:26",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_z5i5fmiw",
"name": "small (3)",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_z5i5fmiw/version/100022",
"plays": 1,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_z5i5fmiw/format/url/protocol/http",
"duration": 6,
"createdAt": "22/11/18 06:41:15",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_0i4hbs9o",
"name": "dolbycanyon",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_0i4hbs9o/version/100012",
"plays": 1,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_0i4hbs9o/format/url/protocol/http",
"duration": 38,
"createdAt": "22/11/18 06:38:13",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_wbd3wu38",
"name": "Mary and little lamb",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_wbd3wu38/version/100002",
"plays": 3,
"categories": "Shows>Sci-Fi,Shows>Regional,Movies>Action",
"categoriesIds": "14,16,21",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_wbd3wu38/format/url/protocol/http",
"duration": 184,
"createdAt": "12/11/18 10:36:17",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_k4unuqi2",
"name": "Johny Johny",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_k4unuqi2/version/100002",
"plays": 2,
"categories": "Shows>Sci-Fi,Movies>Action",
"categoriesIds": "14,21",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_k4unuqi2/format/url/protocol/http",
"duration": 186,
"createdAt": "12/11/18 10:36:17",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_3w4xjt5x",
"name": "One Little Flower",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_3w4xjt5x/version/100012",
"plays": 7,
"categories": "TV>Entertainment",
"categoriesIds": "25",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_3w4xjt5x/format/url/protocol/http",
"duration": 83,
"createdAt": "12/11/18 10:20:14",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_w64yjfen",
"name": "I'm A Little Teapot",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_w64yjfen/version/100012",
"plays": 9,
"categories": "Shows>Action,TV>Shows",
"categoriesIds": "15,11",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_w64yjfen/format/url/protocol/http",
"duration": 104,
"createdAt": "12/11/18 10:20:14",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_591qnfyf",
"name": "Little Jack Horner",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_591qnfyf/version/100002",
"plays": 93,
"categories": "Shows>Regional,Movies>Action,TV>Shows",
"categoriesIds": "16,21,11",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_591qnfyf/format/url/protocol/http",
"duration": 87,
"createdAt": "12/11/18 10:18:49",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_n1wvco0o",
"name": "My School",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_n1wvco0o/version/100012",
"plays": 21,
"categories": "TV>Shows",
"categoriesIds": "11",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_n1wvco0o/format/url/protocol/http",
"duration": 75,
"createdAt": "12/11/18 10:18:49",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_srtxu635",
"name": "Hop a little",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_srtxu635/version/100012",
"plays": 24,
"categories": "Movies>Thriller,Movies>Action,TV>Shows",
"categoriesIds": "23,21,11",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_srtxu635/format/url/protocol/http",
"duration": 108,
"createdAt": "12/11/18 10:18:49",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_uuu7jii2",
"name": "Come Little Children",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_uuu7jii2/version/100002",
"plays": 4,
"categories": "Movies>Sci-Fi,Movies>Action,TV>Shows",
"categoriesIds": "24,21,11",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_uuu7jii2/format/url/protocol/http",
"duration": 94,
"createdAt": "12/11/18 10:16:00",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_9ld6kzph",
"name": "story telling",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_9ld6kzph/version/100002",
"plays": 2,
"categories": "Movies>Drama,Movies>Action",
"categoriesIds": "22,21",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_9ld6kzph/format/url/protocol/http",
"duration": 73,
"createdAt": "12/11/18 10:16:00",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_cz6egmc9",
"name": "Tenali Raman In English",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_cz6egmc9/version/100002",
"plays": 1,
"categories": "Movies>Action,Shows>Thriller",
"categoriesIds": "21,13",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_cz6egmc9/format/url/protocol/http",
"duration": 151,
"createdAt": "12/11/18 10:16:00",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_pj8ansdd",
"name": "The two frogs",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_pj8ansdd/version/100002",
"plays": 1,
"categories": "News>TV9,Movies>Action",
"categoriesIds": "17,21",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_pj8ansdd/format/url/protocol/http",
"duration": 141,
"createdAt": "12/11/18 10:16:00",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_7wy5icdl",
"name": "Twinkle Twinkle",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_7wy5icdl/version/100002",
"plays": 1,
"categories": "News>CNBC",
"categoriesIds": "20",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_7wy5icdl/format/url/protocol/http",
"duration": 181,
"createdAt": "12/11/18 10:16:00",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_0l7xkwee",
"name": "Two little hands",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_0l7xkwee/version/100002",
"plays": 5,
"categories": "Movies>Action,News>BBC",
"categoriesIds": "21,19",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_0l7xkwee/format/url/protocol/http",
"duration": 59,
"createdAt": "12/11/18 10:16:00",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_58md9gkm",
"name": "amazoneco",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_58md9gkm/version/100002",
"plays": 0,
"categories": "News>PublicTV",
"categoriesIds": "18",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_58md9gkm/format/url/protocol/http",
"duration": 36,
"createdAt": "05/11/18 12:01:44",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_f90klu3n",
"name": "arunicecream",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_f90klu3n/version/100012",
"plays": 2,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_f90klu3n/format/url/protocol/http",
"duration": 15,
"createdAt": "05/11/18 12:01:44",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_hmr8atha",
"name": "bahubalitrailer",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_hmr8atha/version/100012",
"plays": 11,
"categories": "TV>Songs,Shows>Thriller,Shows>Regional",
"categoriesIds": "12,13,16",
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_hmr8atha/format/url/protocol/http",
"duration": 21,
"createdAt": "05/11/18 12:01:44",
"status": 2,
"statusc": 2,
"mediaType": 1
},
{
"id": "0_kzwqw8eq",
"name": "BigData",
"thumbnailUrl": "http://onnet-video-platform/p/102/sp/10200/thumbnail/entry_id/0_kzwqw8eq/version/100012",
"plays": 1,
"categories": null,
"categoriesIds": null,
"dataUrl": "http://159.65.156.204/p/102/sp/10200/playManifest/entryId/0_kzwqw8eq/format/url/protocol/http",
"duration": 93,
"createdAt": "05/11/18 12:01:44",
"status": 2,
"statusc": 2,
"mediaType": 1
}
],
"totalRecord": 30
}
答案 0 :(得分:1)
无需为Dio使用Json Decode。您可以执行以下操作:
Response responseval=await dio.post(url,options: Options(headers: headers));
var name=responseval.data['username'];
var id=responseeval.data['id'];
答案 1 :(得分:0)
将Bean或Dio转换成这样的响应,或者您可以使用this将JSON
响应转换为dart
bean
import 'dart:convert';
Welcome welcomeFromJson(String str) {
final jsonData = json.decode(str);
return Welcome.fromJson(jsonData);
}
String welcomeToJson(Welcome data) {
final dyn = data.toJson();
return json.encode(dyn);
}
class Welcome {
int status;
List<Datum> data;
int totalRecord;
Welcome({
this.status,
this.data,
this.totalRecord,
});
factory Welcome.fromJson(Map<String, dynamic> json) => new Welcome(
status: json["status"] == null ? null : json["status"],
data: json["data"] == null ? null : new List<Datum>.from(json["data"].map((x) => Datum.fromJson(x))),
totalRecord: json["totalRecord"] == null ? null : json["totalRecord"],
);
Map<String, dynamic> toJson() => {
"status": status == null ? null : status,
"data": data == null ? null : new List<dynamic>.from(data.map((x) => x.toJson())),
"totalRecord": totalRecord == null ? null : totalRecord,
};
}
class Datum {
String id;
String name;
String thumbnailUrl;
int plays;
String categories;
String categoriesIds;
String dataUrl;
int duration;
String createdAt;
int status;
int statusc;
int mediaType;
Datum({
this.id,
this.name,
this.thumbnailUrl,
this.plays,
this.categories,
this.categoriesIds,
this.dataUrl,
this.duration,
this.createdAt,
this.status,
this.statusc,
this.mediaType,
});
factory Datum.fromJson(Map<String, dynamic> json) => new Datum(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
thumbnailUrl: json["thumbnailUrl"] == null ? null : json["thumbnailUrl"],
plays: json["plays"] == null ? null : json["plays"],
categories: json["categories"] == null ? null : json["categories"],
categoriesIds: json["categoriesIds"] == null ? null : json["categoriesIds"],
dataUrl: json["dataUrl"] == null ? null : json["dataUrl"],
duration: json["duration"] == null ? null : json["duration"],
createdAt: json["createdAt"] == null ? null : json["createdAt"],
status: json["status"] == null ? null : json["status"],
statusc: json["statusc"] == null ? null : json["statusc"],
mediaType: json["mediaType"] == null ? null : json["mediaType"],
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"thumbnailUrl": thumbnailUrl == null ? null : thumbnailUrl,
"plays": plays == null ? null : plays,
"categories": categories == null ? null : categories,
"categoriesIds": categoriesIds == null ? null : categoriesIds,
"dataUrl": dataUrl == null ? null : dataUrl,
"duration": duration == null ? null : duration,
"createdAt": createdAt == null ? null : createdAt,
"status": status == null ? null : status,
"statusc": statusc == null ? null : statusc,
"mediaType": mediaType == null ? null : mediaType,
};
}
此方法将响应您的响应bean对象
Future<Welcome> getMediaList() async {
String url='http://xxxxxx:xxx/onnet_api/mediaList.php';
http.Response res = await http.get(url);
final jsonData = json.decode(res.body);
var map=Map<String, dynamic>.from(jsonData);
var response=Welcome.fromJson(map);
if (res.statusCode == 200) {
return response;
} else {
throw Exception('Failed to load post');
}
}
像这样调用此方法
getMediaList().then((onValue){// on value is an object ot bean class //here you will be able to get members of your bean class })
另一种致电服务的方式是FutureBuilder<Welcome>();
您可以在http.post
帖子网址中添加标题和正文。
Future<UserResponse> loginService() async {
String qaurl = "https://www.xxxxxxxxx.com/api/loginapi.html";
var body = new Map<String, String>(); //here i defined Map ty
body['email'] =_email; // pe
body['password'] = _password;
body['return'] = 'json';
body['apikey'] = ApiKey;
http.Response res = await http.post(qaurl, body: body);
final jsonData = json.decode(res.body);
var map=Map<String, dynamic>.from(jsonData);
var loginResponse=UserResponse.fromJsonMap(map);
if (res.statusCode == 200) {
return loginResponse;
} else {
Navigator.of(context, rootNavigator: true).pop('dialog');
throw Exception('Failed to load post');
}
}