内容纲要
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

void main() async {
  print('Hello, World!');

  // var url = 'https://www.baidu.com/';
  // http.get(url).then((response) {
  //   print(response.body);
  // });

  // var url = 'https://bbs.wacai.com/api2/topic/get_list';
  // http.post(url, body: 'platform=70').then((response) {
  //   print(response.body);
  // });

  print('\n\n\n');

  // url = 'https://bbs.wacai.com/api2/recommend/get_recommend_feeds_v2';
  // http.post(url, body: 'lastTime=&mc=21000020&pagesize=20').then((response) {
  //   print('\n\n\n');
  //   print(response.body);
  // });

  // url = 'https://bbs.wacai.com/api2/tag/get_recommend_tag_with_mc';
  // http.post(url, body: 'mc=21000020').then((response) {
  //   print('\n\n\n');
  //   print(response.body);
  // });

  // url = 'https://bbs.wacai.com/api2/qa/get_recommend_question';
  // http.post(url, body: 'page=1&pageSize=6').then((response) {
  //   print('\n\n\n');
  //   print(response.body);
  // });

  // getPosts().then((result) {
  //   print(result);
  // });

  Api.getPosts().then((posts) {
    posts.forEach((post) {
      print(post.id);
      print(post.title);
      //print(post.categories);
      //print(post.tags);
      print(post.author.name);
      post.categories.forEach((category) {
        print(category.name);
      });

      post.tags.forEach((tag) {
        print(tag.name);
      });

      print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
    });
  });
}

Future getPosts() async {
  String Url = "https://2018.kanpur.wordcamp.org/";
  String api = "wp-json/wp/v2/";
  List posts;

  var res = await http.get(Uri.encodeFull(Url + api + "posts?_embed"),
      headers: {"Accept": "application/json"});

  //print(res.body);

  var filePosts = await File('filePosts.json').writeAsString(res.body);

  var resBody = json.decode(res.body);
  posts = resBody;

  //var filePosts2 = await File('filePosts2.json').writeAsBytes(posts);

  print(posts.length);
  print(posts);

  return "Success!";
}

class Author {
  int id;
  String name;

  Author({
    this.id,
    this.name,
  });

  factory Author.fromJson(Map parsedJson) {
    return new Author(
      id: parsedJson["id"] as int,
      name: parsedJson["name"] as String,
    );
  }

  Map toJson() => {
        'id': id,
        'name': name,
      };
}

class Category {
  int id;
  String name;

  Category({
    this.id,
    this.name,
  });

  factory Category.fromJson(Map parsedJson) {
    return new Category(
      id: parsedJson["id"] as int,
      name: parsedJson["name"] as String,
    );
  }

  Map toJson() => {
        'id': id,
        'name': name,
      };
}

class Tag {
  int id;
  String name;

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

  factory Tag.fromJson(Map parsedJson) {
    return new Tag(
      id: parsedJson["id"] as int,
      name: parsedJson["name"] as String,
    );
  }

  Map toJson() => {
        'id': id,
        'name': name,
      };
}

class Post {
  int id;
  String type;
  String date;
  String modified;
  String title;
  String content;
  //List categories;
  //List tags;
  Author author;
  List categories;
  List tags;

  Post({
    this.id,
    this.type,
    this.date,
    this.modified,
    this.title,
    this.content,
    //this.categories,
    this.tags,
    this.author,
    this.categories,
  });

  factory Post.fromJson(Map parsedJson) {
    return new Post(
      id: parsedJson["id"] as int,
      type: parsedJson["type"] as String,
      date: parsedJson["date"] as String,
      modified: parsedJson["modified"] as String,
      title: parsedJson["title"]["rendered"] as String,
      content: parsedJson["content"]["rendered"] as String,
      //categories: new List.from(parsedJson["categories"]),
      //tags: new List.from(parsedJson["tags"]),
      author: Author.fromJson(parsedJson["_embedded"]["author"][0]),
      categories: new List.from(parsedJson["_embedded"]["wp:term"][0]
          .map((x) => Category.fromJson(x))),
      tags: new List.from(
          parsedJson["_embedded"]["wp:term"][1].map((x) => Tag.fromJson(x))),
    );
  }

  Map toJson() => {
        'id': id,
        'title': title,
        'content': content,
        'author': author.toJson(),
      };
}

class Api {
  static const base_url = "https://www.dev798.com/wp-json";
  static Future> getPosts() async {
    final response = await http.get(
        "${base_url}/wp/v2/posts?_embed&page=1&per_page=10",
        headers: {'Accept': 'application/json'});
    final jsonData = json.decode(response.body);
    return new List.from(jsonData.map((x) => Post.fromJson(x)));
  }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注