Android项目中实现解析JSON数据的方法
这期内容当中小编将会给大家带来有关Android项目中实现解析JSON数据的方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联公司是一家集网站建设,峨山县企业网站建设,峨山县品牌网站建设,网站定制,峨山县网站建设报价,网络营销,网络优化,峨山县网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
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"?><?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
Android原生态代码解析
public class MainActivity extends AppCompatActivity { private ListView lv_json_main; private Listfqs = 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 Listfqs = 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 Listfqs = 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 Listfqs; public BigFQ() { } public BigFQ(String clazz, List 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 getFqs() { return fqs; } public void setFqs(List 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; }
添加联网权限
上述就是小编为大家分享的Android项目中实现解析JSON数据的方法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
分享名称:Android项目中实现解析JSON数据的方法
分享网址:http://scyanting.com/article/ihcdop.html