Android之解析JSON數(shù)據(jù)示例(android原生態(tài),F(xiàn)astJson,Gson)
1.json網(wǎng)頁代碼
<%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%><%@page isELIgnored="false" %>${fqs }
2.json數(shù)據(jù)網(wǎng)頁效果圖
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數(shù)據(jù)" 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數(shù)據(jù)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數(shù)據(jù)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原生態(tài)代碼解析
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 { //獲取數(shù)據(jù)前 @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); } //使用原生態(tài)解析JSON數(shù)據(jù) 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; } //獲取數(shù)據(jù)后更新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 { //獲取數(shù)據(jù)前 @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數(shù)據(jù) 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; } //獲取數(shù)據(jù)后更新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 { //獲取數(shù)據(jù)前 @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數(shù)據(jù) 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; } //獲取數(shù)據(jù)后更新UI @Override protected void onPostExecute(Object o) { super.onPostExecute(o); progressDialog.cancel(); myadapter.notifyDataSetChanged(); } } }
實(shí)體類
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; }
添加聯(lián)網(wǎng)權(quán)限
<!--添加聯(lián)網(wǎng)權(quán)限--> <uses-permission android:name="android.permission.INTERNET" />
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程獲取并設(shè)置Activity亮度的方法
這篇文章主要介紹了Android編程獲取并設(shè)置Activity亮度的方法,涉及Android針對屏幕亮度的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12Android利用BitMap獲得圖片像素數(shù)據(jù)的方法
這篇文章主要介紹了Android利用BitMap獲得圖片像素數(shù)據(jù)的方法,結(jié)合實(shí)例對比分析了Android獲取圖片像素數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-02-02Android報錯Didn‘t?find?class?“android.view.x“問題解決原理剖析
這篇文章主要為大家介紹了Android報錯Didn‘t?find?class?“android.view.x“問題解決及原理剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽詳解
本文主要介紹了Android中wifi與數(shù)據(jù)流量的切換監(jiān)聽的方法步驟。具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01Java4Android開發(fā)教程(一)JDK安裝與配置
本文是Android開發(fā)系列教程的第一篇,主要為大家?guī)淼氖情_發(fā)環(huán)境的準(zhǔn)備工作,JDK安裝與配置圖文教程,非常的詳細(xì),有需要的朋友可以參考下2014-10-10提升Android應(yīng)用視覺吸引效果的10個UI設(shè)計技巧
在Android應(yīng)用開發(fā)中,風(fēng)格和設(shè)計或許不是最關(guān)鍵的要素,但它們在決定Android應(yīng)用成功與否上確實(shí)扮演重要的角色,以下是10個Android應(yīng)用的UI設(shè)計技巧,還有個附加技巧,感興趣的朋友可以了解下哦2013-01-01Android實(shí)現(xiàn)Tab切換界面功能詳解
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)Tab切換界面的功能,以及對Tab變化事件進(jìn)行監(jiān)聽。文中示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05