当前位置: 移动技术网 > IT编程>移动开发>Android > Android Studio获取网络JSON数据并处理的方法

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

秋田犬图片,李思雅,滦县征婚

本文实例为大家分享了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();
    }
  });
}

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网