栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android Studio获取网络JSON数据并处理的方法

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android Studio获取网络JSON数据并处理的方法

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

1、需要的网络JSON数据

2、数据实现类

package chenglong.activitytest.pengintohospital.entity;

import org.json.JSONException;
import org.json.JSONObject;



public class BasSection {
  public Integer id;//科室id
  public String sectionName;//科室名称
  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getSectionName() {
    return sectionName;
  }

  public void setSectionName(String sectionName) {
    this.sectionName = sectionName;
  }

  public BasSection(Integer id, String sectionName){
    this.id = id;
    this.sectionName = sectionName;
  }

  public static BasSection sectionData(JSonObject json){
    try {
      return new BasSection(
     json.getInt("id"),
     json.getString("sectionName");
    } catch (JSonException e) {
      e.printStackTrace();
    }
    return null;
  }
}

3、创建List集合接收数据

List listBasSection = new ArrayList<>();

4、获取JSON数据


public void findSectionData(){
  AsyncHttpClient client = new AsyncHttpClient();
  //你的JSON数据链接地址
  client.get(AbAppConfig.DATA_URL + "appGVConsultation/findSectionData", null, new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
      try {
 JSonObject object = new JSonObject(new String(responseBody));//获取json数据
 JSonArray jsonArray = object.getJSonArray("obj");//获取数据集名称为obj的数据
 Log.d("jsonArray数据输出:", String.valueOf(jsonArray));
 for (int i = 0; i < jsonArray.length();i++) {
   BasSection novels = BasSection.sectionData(jsonArray.getJSonObject(i));//把数据存在novels集合中
   if (novels != null){
     listBasSection.add(novels);
   }
 }

      } catch (JSonException e) {
 Toast.makeText(GV_Consultation.this, "数据请求失败,请稍后重试", Toast.LENGTH_SHORT).show();
      }
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
      //请求失败的回调处理
      Toast.makeText(GV_Consultation.this, "请链接网络,稍后重试", Toast.LENGTH_SHORT).show();
    }
  });
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号