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

发布时间:2020-10-16 23:20:04 作者:龙魂学者
来源:脚本之家 阅读:279

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

1、需要的网络JSON数据

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

2、数据实现类

package chenglong.activitytest.pengintohospital.entity;

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

/**
 *
 * 科室
 * Created by LICHENGLONG on 2017-10-02.
 */

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<BasSection> listBasSection = new ArrayList<>();

4、获取JSON数据

/**
 * 获取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();
    }
  });
}

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

推荐阅读:
  1. Android Studio常用插件有哪些
  2. 浅谈Android项目----JSON解析(4种解析技术详解)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android studio json

上一篇:iOS动画-定时对UIView进行翻转和抖动的方法

下一篇:区分C++中的&和&&

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》