欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-06-11 08:11:25|栏目:Android代码|点击:

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

1、需要的网络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();
    }
  });
}

上一篇:Android 自定义View实现芝麻分曲线图效果

栏    目:Android代码

下一篇:Android在代码中设置沉浸式布局的方法

本文标题:Android Studio获取网络JSON数据并处理的方法

本文地址:http://www.codeinn.net/misctech/139934.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有