<%@page isELIgnored="fal"/>

文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Android之解析JSON数据示例(android原生态,FastJson,Gson)

2022-06-06 04:15

关注

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使用OKHTTP解析JSON数据的实例代码android JSON解析数据 android解析天气预报Android 中对JSON数据解析实例代码Android解析json数据示例代码(三种方式)Android编程实现根据经纬度查询地址并对获取的json数据进行解析的方法Android json数据解析详解及实例代码Android系列---JSON数据解析的实例Android利用Gson解析嵌套多层的Json的简单方法Android M(6.x)使用OkHttp包解析和发送JSON请求的教程Android解析JSON数据的方法分析Android json解析及简单例子android解析JSON数据Android中gson、jsonobject解析JSON的方法详解Android解析json数组对象的方法及Apply和数组的三个技巧Android随手笔记44之JSON数据解析Android学习笔记45之gson解析jsonandroid原生JSON解析实例


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯