Android之解析JSON数据示例(android原生态,FastJson,Gson)
程序员文章站
2023-12-13 11:23:04
1.json网页代码
<%@ page language="java" contenttype="text/plain; charset=utf-8"...
1.json网页代码
<%@ page language="java" contenttype="text/plain; charset=utf-8" pageencoding="utf-8"%><%@page iselignored="false" %>${fqs }
2.json数据网页效果图
3.android代码
布局
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取json数据" android:onclick="getjson"/> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_json_main"></listview> </linearlayout> <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main_fastjson" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取json数据fastjson" android:onclick="getfastjson"/> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_fastjson_main"></listview> </linearlayout> <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main_gson" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="获取json数据gson" android:onclick="getgson"/> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/lv_gson_main"></listview> </linearlayout> <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/tv_item_listview_name" android:layout_weight="1"/> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/tv_item_listview_content" android:layout_weight="1"/> <textview android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/tv_item_listview_time" android:layout_weight="1"/> </linearlayout>
android原生态代码解析
public class mainactivity extends appcompatactivity { private listview lv_json_main; private list<fq> fqs = new arraylist<>(); private myadapter myadapter; private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lv_json_main = (listview) findviewbyid(r.id.lv_json_main); myadapter = new myadapter(); lv_json_main.setadapter(myadapter); progressdialog = new progressdialog(this); progressdialog.setmessage("小青正在拼命加載中....."); } class myadapter extends baseadapter { @override public int getcount() { return fqs.size(); } @override public object getitem(int position) { return fqs.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = layoutinflater.from(mainactivity.this).inflate(r.layout.item_list, null); itemtag itemtag = new itemtag(); itemtag.tv_name = (textview) convertview.findviewbyid(r.id.tv_item_listview_name); itemtag.tv_content = (textview) convertview.findviewbyid(r.id.tv_item_listview_content); itemtag.tv_tiem = (textview) convertview.findviewbyid(r.id.tv_item_listview_time); convertview.settag(itemtag); } itemtag itemtag = (itemtag) convertview.gettag(); itemtag.tv_name.settext(fqs.get(position).getname()); itemtag.tv_content.settext(fqs.get(position).getcontent()); itemtag.tv_tiem.settext(fqs.get(position).gettime()); return convertview; } } public void getjson(view view) { new mytask().execute(); } class mytask extends asynctask { //获取数据前 @override protected void onpreexecute() { super.onpreexecute(); progressdialog.show(); } @override protected object doinbackground(object[] params) { string path = "http://192.168.43.149:8080/datajson.xhtml"; try { url url = new url(path); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setconnecttimeout(5000); if (connection.getresponsecode() == 200) { inputstream is = connection.getinputstream(); //读 bufferedreader br=new bufferedreader(new inputstreamreader(is)); stringbuffer stringbuffer = new stringbuffer(); string str=null; while ((str=br.readline())!=null){ //拼接字符串 stringbuffer.append(str); } //使用原生态解析json数据 jsonobject jsonobject=new jsonobject(stringbuffer.tostring()); string clazz=jsonobject.getstring("clazz"); int lists=jsonobject.getint("lists"); jsonarray jsonarray=jsonobject.getjsonarray("fqs"); for (int i = 0; i < jsonarray.length(); i++) { jsonobject object=jsonarray.getjsonobject(i); string name=object.getstring("name"); string content=object.getstring("content"); string time=object.getstring("time"); fq fq=new fq(name,content,time); fqs.add(fq); } } } catch (exception e) { e.printstacktrace(); } return null; } //获取数据后更新ui @override protected void onpostexecute(object o) { super.onpostexecute(o); progressdialog.cancel(); myadapter.notifydatasetchanged(); } } }
使用fastjson解析
public class mainfastjsonactivity extends appcompatactivity { private listview lv_fastjson_main; private list<fq> fqs = new arraylist<>(); private myadapter myadapter; private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_fastjson); lv_fastjson_main = (listview) findviewbyid(r.id.lv_fastjson_main); myadapter = new myadapter(); lv_fastjson_main.setadapter(myadapter); progressdialog = new progressdialog(this); progressdialog.setmessage("小青正在拼命加載中....."); } class myadapter extends baseadapter { @override public int getcount() { return fqs.size(); } @override public object getitem(int position) { return fqs.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = layoutinflater.from(mainfastjsonactivity.this).inflate(r.layout.item_list, null); itemtag itemtag = new itemtag(); itemtag.tv_name = (textview) convertview.findviewbyid(r.id.tv_item_listview_name); itemtag.tv_content = (textview) convertview.findviewbyid(r.id.tv_item_listview_content); itemtag.tv_tiem = (textview) convertview.findviewbyid(r.id.tv_item_listview_time); convertview.settag(itemtag); } itemtag itemtag = (itemtag) convertview.gettag(); itemtag.tv_name.settext(fqs.get(position).getname()); itemtag.tv_content.settext(fqs.get(position).getcontent()); itemtag.tv_tiem.settext(fqs.get(position).gettime()); return convertview; } } public void getfastjson(view view) { new mytask().execute(); } class mytask extends asynctask { //获取数据前 @override protected void onpreexecute() { super.onpreexecute(); progressdialog.show(); } @override protected object doinbackground(object[] params) { string path = "http://192.168.43.149:8080/datajson.xhtml"; try { url url = new url(path); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setconnecttimeout(5000); if (connection.getresponsecode() == 200) { inputstream is = connection.getinputstream(); //读 bufferedreader br=new bufferedreader(new inputstreamreader(is)); stringbuffer stringbuffer = new stringbuffer(); string str=null; while ((str=br.readline())!=null){ //拼接字符串 stringbuffer.append(str); } //使用fastjson解析json数据 bigfq bigfq=json.parseobject(stringbuffer.tostring(),bigfq.class); string clazz=bigfq.getclazz(); int num=bigfq.getlists(); fqs.addall(bigfq.getfqs()); } } catch (exception e) { e.printstacktrace(); } return null; } //获取数据后更新ui @override protected void onpostexecute(object o) { super.onpostexecute(o); progressdialog.cancel(); myadapter.notifydatasetchanged(); } } }
使用gson解析
public class maingsonactivity extends appcompatactivity { private listview lv_gson_main; private list<fq> fqs = new arraylist<>(); private myadapter myadapter; private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_gson); lv_gson_main = (listview) findviewbyid(r.id.lv_gson_main); myadapter = new myadapter(); lv_gson_main.setadapter(myadapter); progressdialog = new progressdialog(this); progressdialog.setmessage("小青正在拼命加載中....."); } class myadapter extends baseadapter { @override public int getcount() { return fqs.size(); } @override public object getitem(int position) { return fqs.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { convertview = layoutinflater.from(maingsonactivity.this).inflate(r.layout.item_list, null); itemtag itemtag = new itemtag(); itemtag.tv_name = (textview) convertview.findviewbyid(r.id.tv_item_listview_name); itemtag.tv_content = (textview) convertview.findviewbyid(r.id.tv_item_listview_content); itemtag.tv_tiem = (textview) convertview.findviewbyid(r.id.tv_item_listview_time); convertview.settag(itemtag); } itemtag itemtag = (itemtag) convertview.gettag(); itemtag.tv_name.settext(fqs.get(position).getname()); itemtag.tv_content.settext(fqs.get(position).getcontent()); itemtag.tv_tiem.settext(fqs.get(position).gettime()); return convertview; } } public void getgson(view view) { new mytask().execute(); } class mytask extends asynctask { //获取数据前 @override protected void onpreexecute() { super.onpreexecute(); progressdialog.show(); } @override protected object doinbackground(object[] params) { string path = "http://192.168.43.149:8080/datajson.xhtml"; try { url url = new url(path); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setconnecttimeout(5000); if (connection.getresponsecode() == 200) { inputstream is = connection.getinputstream(); //读 bufferedreader br=new bufferedreader(new inputstreamreader(is)); stringbuffer stringbuffer = new stringbuffer(); string str=null; while ((str=br.readline())!=null){ //拼接字符串 stringbuffer.append(str); } //使用gson解析json数据 gson gson=new gson(); bigfq bigfq=gson.fromjson(stringbuffer.tostring(),bigfq.class); string clazz=bigfq.getclazz(); int num=bigfq.getlists(); fqs.addall(bigfq.getfqs()); log.i("哈哈","6"); } } catch (exception e) { e.printstacktrace(); } return null; } //获取数据后更新ui @override protected void onpostexecute(object o) { super.onpostexecute(o); progressdialog.cancel(); myadapter.notifydatasetchanged(); } } }
实体类
public class bigfq { private string clazz; private int lists; private list<fq> fqs; public bigfq() { } public bigfq(string clazz, list<fq> fqs, int lists) { this.clazz = clazz; this.fqs = fqs; this.lists = lists; } public string getclazz() { return clazz; } public void setclazz(string clazz) { this.clazz = clazz; } public list<fq> getfqs() { return fqs; } public void setfqs(list<fq> fqs) { this.fqs = fqs; } public int getlists() { return lists; } public void setlists(int lists) { this.lists = lists; } } public class fq { private string name; private string content; private string time; public fq(){} public fq(string name, string time, string content) { this.name = name; this.time = time; this.content = content; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public string gettime() { return time; } public void settime(string time) { this.time = time; } } public class itemtag { public textview tv_name; public textview tv_content; public textview tv_tiem; }
添加联网权限
<!--添加联网权限--> <uses-permission android:name="android.permission.internet" />
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android之解析JSON数据示例(android原生态,FastJson,Gson)
-
Android编程简单解析JSON格式数据的方法示例
-
Android中使用Gson解析JSON数据的两种方法
-
Android解析json数据示例代码(三种方式)
-
Android编程简单解析JSON格式数据的方法示例
-
Android解析json数据示例代码(三种方式)
-
Android 中Json解析的几种框架(Gson、Jackson、FastJson、LoganSquare)使用与对比
-
Android数据解析JSON解析之FastJson解析
-
Android开发之原生解析复杂的json数据
-
Android 之 GSON解析JSON数据