Android 网络数据JSON解析使用(建议收藏)

<TextView

android:id=“@+id/joke_tv_title”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center”

android:text=“title”

android:textColor=“@android:color/black”

android:textSize=“18sp” />

<TextView

android:id=“@+id/joke_tv_content”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“5dp”

android:text=“content”

android:textColor=“@android:color/black”

android:textSize=“16sp” />

<ImageView

android:id=“@+id/joke_iv_content”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“5dp”

android:adjustViewBounds=“true”

android:scaleType=“fitXY” />

<TextView

android:id=“@+id/joke_tv_time”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_margin=“5dp”

android:gravity=“right”

android:text=“时间”

android:textSize=“14sp” />

上面的布局是每一个笑话内容显示的布局设计

4.创建一个json解析和数据收集的类

package com.lwz.conn;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import java.io.Serializable;

import java.util.ArrayList;

/**

  • 这是一个数据解析的工具类,

  • 这里是从源码中取出属性值

  • 这里是使用从外到内一层一层的去获取资源数据,最里面一层的数据才是我们要显示在页面上的数据

  • 这里的处理的方式是比较麻烦的一种,其中它处理的方式可以有好几种

  • 这里的数据层层嵌套,就像xml资源的子标签一样

*/

/*下面是提供API网站给我们的数据的提示,xml文件中下面的某些变量,我们可以选择性的去使用,

但是这些变量的名字是固定的,更改了就获取不到了

allNum String 345 所有记录数

allPages String 45 所有页数。每页20条。

contentlist JokeItem[] 笑话条目内容

  • title String 吃了一百年的玉米 笑话标题

  • text String 笑话正文

  • ct String 2015-07-10 05:54:00.000 生成时间

currentPage String 1 当前第几页

maxResult String 20 每页最多显示几条*/

//因为要Intent来实现数据的传递,所以要实现序列化

public class JokeBean implements Serializable {

//最外层的属性值

int showapi_res_code;

String showapi_res_error;

Body showapi_res_body;//这个才是有用的信息数据,上面的实现其实也是可以不用去定义的

@Override

public String toString() {

return “JokeBean{” +

“showapi_res_body=” + showapi_res_body +

“, showapi_res_code=” + showapi_res_code +

“, showapi_res_error='” + showapi_res_error + ‘’’ +

‘}’;

}

class Body implements Serializable {

int allNum;

int allPages;

int currentPage;//当前第几页

int maxResult;

int ret_code;

ArrayList contentlist;

@Override

public String toString() {

return “Body{” +

“allNum=” + allNum +

“, allPages=” + allPag

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值