Flutter http.get 未获取正文数据

问题描述

我正在尝试从我的 json 提要中获取数据,这就是 Json

但是当我尝试使用 body 结果时它不起作用(但它可以与其他 json 一起使用,例如 https://jsonplaceholder.typicode.com/users),我的代码如下所示:

import 'package:Flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'noticias.dart';

class Services {
static const String url =
  'https://studiofutbol.com.ec/api/json/Feed_master.PHP?pagina=';

static Future<Portada> getNoticias() async {
try {
  var headers = {
    'Content-Type': 'application/x-www-form-urlencoded','X-Requested-With': 'XMLHttpRequest',};
  final response = await http.get(url,headers: headers);
  debugPrint(response.body);
  if (200 == response.statusCode) {
    final Portada noticiasportada = portadaFromJson(response.body);
    return noticiasportada;
  } else {
    return Portada();
  }
} catch (e) {
  return Portada();
}
  }
}

添加了这些标题,但没有帮助,可能是什么导致了问题?

解决方法

它像这样对我有用

Future<Portada> getNoticias() async {
    try {
      HttpClient httpClient = new HttpClient();
      HttpClientRequest request = await httpClient.getUrl(Uri.parse(
          "https://studiofutbol.com.ec/api/json/feed_master.php?pagina="));
      request.headers.set('content-type','application/json');
     
      HttpClientResponse response = await request.close();
      String reply = await response.transform(utf8.decoder).join();
      print(reply);
      httpClient.close();
      Map<String,dynamic> map = json.decode(reply);
      final Portada noticiasportada = Portada.fromMap(map);
      return noticiasportada;
    } catch (e) {
      return Portada();
    }
  }

模型类

import 'dart:convert';

Portada portadaFromMap(String str) => Portada.fromMap(json.decode(str));

String portadaToMap(Portada data) => json.encode(data.toMap());

class Portada {
  Portada({
    this.json,this.menu,this.banners,this.tags,this.equipos,this.menuIos,this.version,this.versionIos,});

  List<Json> json;
  List<Menu> menu;
  Banners banners;
  List<Tag> tags;
  List<Equipo> equipos;
  List<MenuIo> menuIos;
  String version;
  String versionIos;

  factory Portada.fromMap(Map<String,dynamic> json) => Portada(
    json: json["Json"] == null ? null : List<Json>.from(json["Json"].map((x) => Json.fromMap(x))),menu: json["menu"] == null ? null : List<Menu>.from(json["menu"].map((x) => Menu.fromMap(x))),banners: json["banners"] == null ? null : Banners.fromMap(json["banners"]),tags: json["tags"] == null ? null : List<Tag>.from(json["tags"].map((x) => Tag.fromMap(x))),equipos: json["equipos"] == null ? null : List<Equipo>.from(json["equipos"].map((x) => Equipo.fromMap(x))),menuIos: json["menu_ios"] == null ? null : List<MenuIo>.from(json["menu_ios"].map((x) => MenuIo.fromMap(x))),version: json["version"] == null ? null : json["version"],versionIos: json["version_ios"] == null ? null : json["version_ios"],);

  Map<String,dynamic> toMap() => {
    "Json": json == null ? null : List<dynamic>.from(json.map((x) => x.toMap())),"menu": menu == null ? null : List<dynamic>.from(menu.map((x) => x.toMap())),"banners": banners == null ? null : banners.toMap(),"tags": tags == null ? null : List<dynamic>.from(tags.map((x) => x.toMap())),"equipos": equipos == null ? null : List<dynamic>.from(equipos.map((x) => x.toMap())),"menu_ios": menuIos == null ? null : List<dynamic>.from(menuIos.map((x) => x.toMap())),"version": version == null ? null : version,"version_ios": versionIos == null ? null : versionIos,};
}

class Banners {
  Banners({
    this.splash,this.splashiphone,this.main,this.interna,});

  Interna splash;
  Interna splashiphone;
  Interna main;
  Interna interna;

  factory Banners.fromMap(Map<String,dynamic> json) => Banners(
    splash: json["splash"] == null ? null : Interna.fromMap(json["splash"]),splashiphone: json["splashiphone"] == null ? null : Interna.fromMap(json["splashiphone"]),main: json["main"] == null ? null : Interna.fromMap(json["main"]),interna: json["interna"] == null ? null : Interna.fromMap(json["interna"]),dynamic> toMap() => {
    "splash": splash == null ? null : splash.toMap(),"splashiphone": splashiphone == null ? null : splashiphone.toMap(),"main": main == null ? null : main.toMap(),"interna": interna == null ? null : interna.toMap(),};
}

class Interna {
  Interna({
    this.imagen,this.link,});

  String imagen;
  String link;

  factory Interna.fromMap(Map<String,dynamic> json) => Interna(
    imagen: json["imagen"] == null ? null : json["imagen"],link: json["link"] == null ? null : json["link"],dynamic> toMap() => {
    "imagen": imagen == null ? null : imagen,"link": link == null ? null : link,};
}

class Equipo {
  Equipo({
    this.id,this.nombre,});

  int id;
  String nombre;

  factory Equipo.fromMap(Map<String,dynamic> json) => Equipo(
    id: json["id"] == null ? null : json["id"],nombre: json["nombre"] == null ? null : json["nombre"],dynamic> toMap() => {
    "id": id == null ? null : id,"nombre": nombre == null ? null : nombre,};
}

class Json {
  Json({
    this.id,this.title,this.titleint,this.permalink,this.content,this.excerpt,this.date,this.author,this.image,this.fuente,this.thumbnail,this.categories,this.contentIphone,this.videoEnlace,});

  int id;
  String title;
  String titleint;
  String permalink;
  String content;
  String excerpt;
  DateTime date;
  String author;
  List<dynamic> image;
  String fuente;
  String thumbnail;
  List<String> categories;
  List<String> tags;
  String contentIphone;
  String videoEnlace;

  factory Json.fromMap(Map<String,dynamic> json) => Json(
    id: json["id"] == null ? null : json["id"],title: json["title"] == null ? null : json["title"],titleint: json["titleint"] == null ? null : json["titleint"],permalink: json["permalink"] == null ? null : json["permalink"],content: json["content"] == null ? null : json["content"],excerpt: json["excerpt"] == null ? null : json["excerpt"],date: json["date"] == null ? null : DateTime.parse(json["date"]),author: json["author"] == null ? null : json["author"],image: json["image"] == null ? null : List<dynamic>.from(json["image"].map((x) => x)),fuente: json["fuente"] == null ? null : json["fuente"],thumbnail: json["thumbnail"] == null ? null : json["thumbnail"],categories: json["categories"] == null ? null : List<String>.from(json["categories"].map((x) => x)),tags: json["tags"] == null ? null : List<String>.from(json["tags"].map((x) => x)),contentIphone: json["contentIphone"] == null ? null : json["contentIphone"],videoEnlace: json["video_enlace"] == null ? null : json["video_enlace"],"title": title == null ? null : title,"titleint": titleint == null ? null : titleint,"permalink": permalink == null ? null : permalink,"content": content == null ? null : content,"excerpt": excerpt == null ? null : excerpt,"date": date == null ? null : date.toIso8601String(),"author": author == null ? null : author,"image": image == null ? null : List<dynamic>.from(image.map((x) => x)),"fuente": fuente == null ? null : fuente,"thumbnail": thumbnail == null ? null : thumbnail,"categories": categories == null ? null : List<dynamic>.from(categories.map((x) => x)),"tags": tags == null ? null : List<dynamic>.from(tags.map((x) => x)),"contentIphone": contentIphone == null ? null : contentIphone,"video_enlace": videoEnlace == null ? null : videoEnlace,};
}

class Menu {
  Menu({
    this.url,});

  String url;
  String nombre;

  factory Menu.fromMap(Map<String,dynamic> json) => Menu(
    url: json["url"] == null ? null : json["url"],dynamic> toMap() => {
    "url": url == null ? null : url,};
}

class MenuIo {
  MenuIo({
    this.nombre,this.url,this.segue,this.img,});

  String nombre;
  String url;
  String segue;
  String img;

  factory MenuIo.fromMap(Map<String,dynamic> json) => MenuIo(
    nombre: json["nombre"] == null ? null : json["nombre"],url: json["url"] == null ? null : json["url"],segue: json["segue"] == null ? null : json["segue"],img: json["img"] == null ? null : json["img"],dynamic> toMap() => {
    "nombre": nombre == null ? null : nombre,"url": url == null ? null : url,"segue": segue == null ? null : segue,"img": img == null ? null : img,};
}

class Tag {
  Tag({
    this.id,});

  int id;
  String nombre;
  String url;

  factory Tag.fromMap(Map<String,dynamic> json) => Tag(
    id: json["id"] == null ? null : json["id"],};
}