位置: 编程技术 - 正文

[个人学习笔记]Android 从网络获取json并解析 --Demo

编辑:rootadmin

推荐整理分享[个人学习笔记]Android 从网络获取json并解析 --Demo,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

//网络获取json的方法public String getJsonFromInternet(String url) throws MalformedURLException,IOException {String resultString = "";InputStreamReader isrInputStreamReader = new InputStreamReader(new URL(url).openStream());//从网上获取json的字节流BufferedReader brBufferedReader = new BufferedReader(isrInputStreamReader);//将字节流转换成字符流String line = "";while ((line = brBufferedReader.readLine()) != null) {resultString &#;= line;}return resultString;//读取网上的json&#;式的字符串并返回}//需要特别注意的是获取json的方法需要脱离UI线程使用,//可以使用AsyncTask中doInBackground获取网络jsonString//用json字符串读取内部相关数据{ "weatherinfo": {//需要调用getJSONObject("weatherinfo")来获取下面的数据 "city": "杭州", "city_en": "hangzhou", "date_y": "年6月日", "date": "", "week": "星期四", "fchh": "", "cityid": "", "temp1": "℃~℃", "weather1": "阵雨转阴" }}//这个是从网上获取到的json字符串//从json字符串中获取相关的数据的方法public weather(String json) throws JSONException {// TODO Auto-generated constructor stubJSONObject ja=new JSONObject(json).getJSONObject("weatherinfo");//获取字符串中weatherinfo中的数据并复制给当前的jsonobject对象//这个是获取到jsonstring中只有一个对象数据的情况下获取数据使用的setCity(ja.getString("city"));//从ja对象中获取需要的相关信息setTime(ja.getString("date_y"));setWeek(ja.getString("week"));setWeather(ja.getString("weather1"));setTemp(ja.getString("temp1"));}// 从网上获取json并解析数据后显示在view上的demo//MainActivity.classpackage com.example.testjson;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import org.json.JSONException;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {private weather mw;private TextView date;private TextView city;private TextView weather;private TextView temp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);date = (TextView) findViewById(R.id.date);temp = (TextView) findViewById(R.id.temp);city = (TextView) findViewById(R.id.city);weather = (TextView) findViewById(R.id.weather);new backgetjson(date, city, weather, temp).execute(" String getJsonFromInternet(String url) throws MalformedURLException,IOException {String resultString = "";InputStreamReader isrInputStreamReader = new InputStreamReader(new URL(url).openStream());BufferedReader brBufferedReader = new BufferedReader(isrInputStreamReader);String line = "";while ((line = brBufferedReader.readLine()) != null) {resultString &#;= line;}return resultString;}class backgetjson extends AsyncTask<String, Void, String> {private weather mw;private TextView date;private TextView city;private TextView weather;private TextView temp;public backgetjson(TextView d, TextView c, TextView w, TextView t) {// TODO Auto-generated constructor stubdate = d;city = c;weather = w;temp = t;}@Overrideprotected String doInBackground(String... params) {// TODO Auto-generated method stubString jsString = "";try {jsString = getJsonFromInternet(params[0]);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return jsString;}@Overrideprotected void onPostExecute(String result) {// TODO Auto-generated method stubsuper.onPostExecute(result);try {mw = new weather(result);} catch (JSONException e) {// TODO Auto-generated catch blocke.printStackTrace();}date.setText(mw.getTime());city.setText(mw.getCity());weather.setText(mw.getWeather());temp.setText(mw.getTemp());}}}//weather.classpackage com.example.testjson;import org.json.JSONException;import org.json.JSONObject;public class weather {private String city;private String time;private String week;private String weather;private String temp;public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getTime() {return time;}public void setTime(String time) {this.time = time;}public String getWeek() {return week;}public void setWeek(String week) {this.week = week;}public String getWeather() {return weather;}public void setWeather(String weather) {this.weather = weather;}public String getTemp() {return temp;}public void setTemp(String temp) {this.temp = temp;}public weather() {// TODO Auto-generated constructor stub}public weather(String json) throws JSONException {// TODO Auto-generated constructor stubJSONObject ja = new JSONObject(json).getJSONObject("weatherinfo");setCity(ja.getString("city"));setTime(ja.getString("date_y"));setWeek(ja.getString("week"));setWeather(ja.getString("weather1"));setTemp(ja.getString("temp1"));}}// 如果获取的json字符串中有如下结构的可以解析成jsonarray{ status: 1, data: -[ -{ id: 1, name: "Tony老师聊shell——环境变量配置文件", picSmall: " picBig: " description: "为你带来shell中的环境变量配置文件", learner: }, -{ id: 2, name: "数学知识在CSS动画中的应用", picSmall: " picBig: " description: "数学知识与CSS结合实现酷炫效果", learner: }, -{ id: 3, name: "Oracle数据库开发必备利器之PL/SQL基础", picSmall: " picBig: " description: "Oracle数据库高级开发必备的基础。", learner: }, -{ id: 4, name: "Android见证消息推送时刻进阶篇", picSmall: " picBig: " description: "Android消息推送就在&#;前,Come on", learner: },}//解析代码如下JSONObject ja=new JSONObject(json);JSONArray jsonarray=ja.getJSONArray("data");for (int i=0;i<jsonarray.length();i&#;&#;){ja=jsonarray.getJSONObject(i);.....//这里得到了一个jsonobject//得到一个jsonobject之后可以直接从里面获取数据。//方法与前面获取数据的方法一样可以从中获取类&#;于一个arraylist的对象}

[个人学习笔记]Android 从网络获取json并解析 --Demo

Demo 下载链接:

版权声明:本文为博主原创文章,未经博主允许不得转载。

在ubuntu上搭建android开发环境(1)——安装ubuntu 电脑是年买的,到现在有点老了,运行as比较吃力。所以买了一个G的固态硬盘+8GDDR3,升级完之后,心血来潮,想在ubuntu下配置一个as开发环境。于

Android 执行 gson.toJson(object) 报java.lang.StackOverflowError异常 如下面的代码,执行后报java.lang.StackOverflowError错误:MusicSavedInfomusicSavedInfo=newMusicSavedInfo(currentStartTime,openOrCloseFlag,currentDayFlags,currentSelectMusicTitle,currentSel

Material设计非得靠Android L吗?看过来,自定View仿elevation效果! Material设计中主要就是纸和z轴的概念,如果根据z绘制出阴影效果,就基本实现了elevation效果了。先上个效果图代码不多,效果却不错,在此抛砖引玉,

标签: [个人学习笔记]Android 从网络获取json并解析 --Demo

本文链接地址:https://www.jiuchutong.com/biancheng/370912.html 转载请保留说明!

上一篇:Activity回传值实例(activity传递数据)

下一篇:在ubuntu上搭建android开发环境(1)——安装ubuntu(创建ubuntu)

  • 计提印花税会计凭证怎么做
  • 所得税会计与财务会计比较研究
  • 建筑工程企业人才引进可以引进医药行业吗
  • 物流辅助服务属于什么费用
  • 血液制品3%增值税税率
  • 印花税按照开票金额缴纳可以吗
  • 住宿发票没有天数
  • 房地产开发企业资质等级有几个
  • 营改增 旅游服务
  • 超市送现金券怎么做账
  • 库存商品记错了怎样写分录
  • 领用包装物会计处理
  • 民办幼儿园的经费投入
  • 土地流转承包费拖欠
  • 承兑汇票银行背书
  • 补交以前年度增值税和滞纳金怎么开票
  • 开旅游公司有补贴吗
  • 设备安装税率是6%还是9%
  • 接手新公司涉税问题分析
  • 残疾人保障金工资总额包括社保吗
  • 个人将房产无偿赠与他人应交个人所得税吗
  • 自己生产的水泥用于建造厂房
  • 收到工会经费返还如何做账
  • 系统备份工具
  • 鸿蒙工具箱巅峰模式有什么用
  • 如何设置电源键关闭屏幕
  • 代收的水电费怎么做账
  • bios boot设置
  • schedhlp.exe - schedhlp是什么进程 作用是什么
  • linux 计算字符数
  • 微软surface售后电话人工
  • 香椿的功效与作用百度百科
  • 代购机票骗局
  • 公司简易注销的公示期多长时间
  • windows 平台
  • 属于工业企业期间费用的是
  • web开发 python
  • yolov2模型
  • 《开具红字增值税专用发票信息表》纸质
  • 长期待摊费用科目怎么使用
  • 金税盘中的发票修复是什么意思
  • 车间管理人员为什么是制造费用
  • 退休人员发绩效
  • 计提折旧是怎么计算的
  • 结转生产成品
  • 本月确认收入 发票下月开
  • 在mysql中创建数据库和表作业
  • mongodb中主键的默认格式是哪个?
  • 盈余公积弥补亏损不影响留存收益
  • Centos5.5中安装Mysql5.5过程分享
  • 新冠捐款捐物新闻
  • 工程收到款项会计分录
  • 收到员工生育津贴怎么做分录
  • 学校接受捐赠收入要交企业所得税吗
  • 物业管理单位收取的物业费征不征收增值税
  • 无法收回的应收账款如何进行财务处理
  • 营业成本年末怎么结转
  • 红字发票开错了已上传如何作废?
  • 公司中征码是什么样子的
  • 应交税费应交增值税已交税金怎么用
  • 三栏式明细账适用于原材料吗
  • 会计账簿的设计意义
  • mysql处理
  • sql存储过程实例
  • myeclipse自动生成get set
  • 强化廉洁意识 筑牢思想防线
  • 如何创建ubuntu安装教程
  • 怎样打开windows设置页面
  • centos sudoers
  • asmb进程
  • win10系统中怎么打开IE浏览器
  • androidannotations的background和UiThread配合使用参考
  • shell获取文件内容给变量
  • css中边界margin的多种定义方法
  • jq cookie
  • 安卓hid通信
  • 高通 trinket
  • Javascript字符串对象函数
  • 医保可以直接到税局买吗?
  • 2022年企业所得税税率2.5% 10% 25%
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设