Flutter开发之——网络请求-手动json数据解析

文章讲述了如何在Android开发中使用Json进行数据转换,创建Article、ArticleData和Person类,并展示了如何通过HttpClient进行网络请求并解析返回结果。同时提到了学习资源的整理和提升技能的方法。
摘要由CSDN通过智能技术生成
转换后

3.4 将Json彻底转换为数据类

数据类

class Article {

int errorCode;

String errorMsg;

ArticleData data;

Article(this.errorCode, this.errorMsg, this.data);

Article.formJson(Map<String, dynamic> json) {

Article(

errorCode= json[‘errorCode’],

errorMsg = json[‘errorMsg’],

data = ArticleData.fromJson(json[‘data’])

);

}

}

class ArticleData {

int curPage;

int offset;

bool over;

int pageCount;

int size;

int total;

List datas;

ArticleData(this.curPage, this.offset, this.over, this.pageCount, this.size,

this.total, this.datas);

ArticleData.fromJson(Map<String, dynamic> json) {

var personList = List();

for (Map<String, dynamic> map in json[‘datas’]) {

personList.add(Person.fromJson(map));

}

ArticleData(

curPage = json[‘curPage’],

offset = json[‘curPage’],

over = json[‘over’],

pageCount = json[‘pageCount’],

size = json[‘size’],

total = json[‘total’],

datas = personList);

}

}

class Person {

String apkLink;

int audit;

String author;

bool canEdit;

int chapterId;

String chapterName;

bool collect;

int courseId;

String desc;

String descMd;

String envelopePic;

bool fresh;

String host;

int id;

String link;

String niceDate;

String niceShareDate;

String origin;

String prefix;

String projectLink;

int publishTime;

int realSuperChapterId;

int selfVisible;

int shareDate;

String shareUser;

int superChapterId;

String superChapterName;

List tags;

String title;

int type;

int userId;

int visible;

int zan;

Person(

this.apkLink,

this.audit,

this.author,

this.canEdit,

this.chapterId,

this.chapterName,

this.collect,

this.courseId,

this.desc,

this.descMd,

this.envelopePic,

this.fresh,

this.host,

this.id,

this.link,

this.niceDate,

this.niceShareDate,

this.origin,

this.prefix,

this.projectLink,

this.publishTime,

this.realSuperChapterId,

this.selfVisible,

this.shareDate,

this.shareUser,

this.superChapterId,

this.superChapterName,

this.tags,

this.title,

this.type,

this.userId,

this.visible,

this.zan);

Person.fromJson(Map<String, dynamic> json) {

var tagList = List();

for (Map<String, dynamic> map in json[‘tags’]) {

tagList.add(Tag.fromJson(map));

}

Person(

apkLink = json[‘apkLink’],

audit = json[‘audit’],

author = json[‘author’],

canEdit = json[‘canEdit’],

chapterId = json[‘chapterId’],

chapterName = json[‘chapterName’],

collect = json[‘collect’],

courseId = json[‘courseId’],

desc = json[‘desc’],

descMd = json[‘descMd’],

envelopePic = json[‘envelopePic’],

fresh = json[‘fresh’],

host = json[‘host’],

id = json[‘id’],

link = json[‘link’],

niceDate = json[‘niceDate’],

niceShareDate = json[‘niceShareDate’],

origin = json[‘origin’],

prefix = json[‘prefix’],

projectLink = json[‘projectLink’],

publishTime = json[‘publishTime’],

realSuperChapterId = json[‘realSuperChapterId’],

selfVisible = json[‘selfVisible’],

shareDate = json[‘shareDate’],

shareUser = json[‘shareUser’],

superChapterId = json[‘superChapterId’],

superChapterName = json[‘superChapterName’],

tags = tagList,

title = json[‘title’],

type = json[‘type’],

userId = json[‘userId’],

visible = json[‘visible’],

zan = json[‘zan’]);

}

}

class Tag {

String name;

String url;

Tag(this.name, this.url);

Tag.fromJson(Map<String, dynamic> json) {

Tag(name = json[‘name’], url = json[‘url’]);

}

}

解析后效果图

四 解析结果的包装


4.1 不返回结果的请求

void _httpGet2() async {

var httpClient = new HttpClient();

var uri = Uri(

scheme: ‘https’,

host: ‘www.wanandroid.com’,

path: ‘article/list/0/json’,

);

HttpClientRequest request = await httpClient.getUrl(uri);

HttpClientResponse response = await request.close();

String responseBody = await response.transform(utf8.decoder).join();

var jsonDecode = json.decode(responseBody);

var article = Article.formJson(jsonDecode);

print(article);

}

4.2 返回Article的请求(因为async,自动添加为Future
)

Future

_httpGet2() async {

var httpClient = new HttpClient();

var uri = Uri(

scheme: ‘https’,

host: ‘www.wanandroid.com’,

path: ‘article/list/0/json’,

);

HttpClientRequest request = await httpClient.getUrl(uri);

HttpClientResponse response = await request.close();

String responseBody = await response.transform(utf8.decoder).join();

var jsonDecode = json.decode(responseBody);

var article = Article.formJson(jsonDecode);

print(article);

return article;

}

4.3 赋值

Future

futureArticle; //声明变量

@override

void initState() {

super.initState();

futureArticle=_httpGet2(); //请求结果赋值

}

五 包装数据的显示


5.1 说明

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

面试宝典

面试必问知识点、BATJ历年历年面试真题+解析

学习经验总结

(一)调整好心态
心态是一个人能否成功的关键,如果不调整好自己的心态,是很难静下心来学习的,尤其是现在这么浮躁的社会,大部分的程序员的现状就是三点一线,感觉很累,一些大龄的程序员更多的会感到焦虑,而且随着年龄的增长,这种焦虑感会越来越强烈,那么唯一的解决办法就是调整好自己的心态,要做到自信、年轻、勤奋。这样的调整,一方面对自己学习有帮助,另一方面让自己应对面试更从容,更顺利。

(二)时间挤一挤,制定好计划
一旦下定决心要提升自己,那么再忙的情况下也要每天挤一挤时间,切记不可“两天打渔三天晒网”。另外,制定好学习计划也是很有必要的,有逻辑有条理的复习,先查漏补缺,然后再系统复习,这样才能够做到事半功倍,效果才会立竿见影。

(三)不断学习技术知识,更新自己的知识储备
对于一名程序员来说,技术知识方面是非常重要的,可以说是重中之重。**要面试大厂,自己的知识储备一定要非常丰富,若缺胳膊少腿,别说在实际工作当中,光是面试这一关就过不了。**对于技术方面,首先基础知识一定要扎实,包括自己方向的语言基础、计算机基础、算法以及编程等等。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

觉很累,一些大龄的程序员更多的会感到焦虑,而且随着年龄的增长,这种焦虑感会越来越强烈,那么唯一的解决办法就是调整好自己的心态,要做到自信、年轻、勤奋。这样的调整,一方面对自己学习有帮助,另一方面让自己应对面试更从容,更顺利。

(二)时间挤一挤,制定好计划
一旦下定决心要提升自己,那么再忙的情况下也要每天挤一挤时间,切记不可“两天打渔三天晒网”。另外,制定好学习计划也是很有必要的,有逻辑有条理的复习,先查漏补缺,然后再系统复习,这样才能够做到事半功倍,效果才会立竿见影。

(三)不断学习技术知识,更新自己的知识储备
对于一名程序员来说,技术知识方面是非常重要的,可以说是重中之重。**要面试大厂,自己的知识储备一定要非常丰富,若缺胳膊少腿,别说在实际工作当中,光是面试这一关就过不了。**对于技术方面,首先基础知识一定要扎实,包括自己方向的语言基础、计算机基础、算法以及编程等等。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值