位置: 编程技术 - 正文

[个人学习笔记]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)

  • 怎么查历年
  • 文化建设税减免到什么时候
  • 公司注销还能要回钱吗
  • 预付开发票加油后还能开吗
  • 冲减暂估成本的会计分录
  • 财务人员培训费怎么入账
  • 固定资产清理的金额怎么算
  • 个体户定额交税还用交个税吗
  • 处置无形资产取得的收入计入
  • 公司购买软件著作权
  • 劳保费用可以开专票吗
  • 互联网企业交什么保险
  • 投资协议需要注意细节
  • 利润减库存为什么不等于现金
  • 营业外支出调减应该填在纳税调整项目明细表哪一项
  • 在建工程抵工程款
  • 股权转让股权公告范本
  • 公司名下商品房交易开具发票
  • 软件行业研发费用比例有要求么
  • 涉嫌虚开发票的几种情况
  • 预收款项确认主营业务收入谨慎性
  • 如何查看本机的ip 网关 和dns配置
  • 应付职工薪酬包括个人社保和个税吗
  • 结转本年利润之后还要干啥
  • 免征的增值税怎么做账
  • 财政拨款收入是指行政单位从哪里取得的预算资金
  • PHP:xml_parse_into_struct()的用法_XML解析器函数
  • php utf8转unicode
  • vue前端代码实例
  • linux内核与文件系统
  • zendframework3中文手册
  • 开办费的具体内容有哪些
  • 以前年度损益调整结转到哪里
  • 工程分包合同
  • 微信小程序消息订阅怎么打开
  • dex源码提取
  • 应交税费是借增还是贷增?
  • 金额太小不给开发票
  • 缴税的会计处理
  • 进项税额转出最后怎么处理
  • 业务招待费是否计入管理费用
  • 以前年度损益调整账务处理分录
  • 土地增值税的计算方法公式
  • 计提缴纳企业所的会计分录怎么写
  • 发票冲红和作废有啥区别
  • 公款私存个人检讨
  • 临时工工资由谁发
  • 报销连号发票的合理解释
  • 银行的存单丢失了可以补办吗
  • 公司交的养老保险的钱可以取出来吗
  • 投资损失如何入账
  • 发票上的折扣是什么意思
  • 税收滞纳金什么意思
  • 出售无形资产取得的收入计入什么科目
  • 一般纳税人暂估入库的会计分录怎么写
  • 食品加工企业成本核算方法和流程
  • 特殊事项税务处理
  • 将哪一linux文件系统引入
  • wysafe.exe是什么
  • win7电脑老是跳出弹窗广告怎么办
  • win8系统怎么到桌面
  • xp系统如何设置
  • 如何使用朋友的山姆卡
  • linux收发邮件
  • Windows8和Windows Phone8中IE10两者相同点和区别
  • android开发环境配置
  • linux开发android好处
  • 阿J的cocos2d-x学习笔记-元素消消看(四)-可发展的空间及游戏开发中的问题
  • linux中argv
  • unity2020商店
  • jquery插件开发方法
  • 有哪些小工具
  • python提供三种基本数值类型
  • 生成ssh
  • Activity的生命周期和页面之间的传递
  • javascript获取html元素的方法
  • 如何查询税务情况
  • 河南省教育厅纪检组举报电话
  • 运输服务费税率9%还是6%
  • 重庆市怎么在网上打印章程
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设