![android取得json数据_第1页](http://file3.renrendoc.com/fileroot_temp3/2021-12/31/1211b9bf-37c5-4fe1-b7cc-d958842787bc/1211b9bf-37c5-4fe1-b7cc-d958842787bc1.gif)
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、android取得json数据步骤一:页面返回json数据。对本空间日志“和ajax的容易实现”的json.php代码做一些容易修改,代码如下:将代码 echo $_get'jsoallback'.'('.custom_json:encode($big_test).')'改为 echo $_get'jsoncallback'.custom_json:encode($big_test);将文件另存为jsondata.php。步骤二:编写andro代码。1.编写contact类,设置和get办法。package com.domain;
2、public class contact private sing name; private int age; private int s; private double height; private boolean is_human; private string string; public contact() public contact(string name, int age, int sex, double height, boolean is_human, string string) = name; this.age = age; this.sex =
3、sex; this.height = height; this.is_human = is_human; this.string = string; public string getname() return name;public void setname(string name) = name;public int getage() return age;public void setage(int age) this.age = age;public int getsex() return sex;public void setsex(int sex) this.s
4、ex = sex;public double getheight() return height;public void setheight(double height) this.height = height;public boolean isis_human() return is_human;public void setis_human(boolean is_human) this.is_human = is_human;public string getstring() return string;public void setstring(string string) this.
5、string = string;2.编写输入流处理工具类streamtoo。package com.shao.utils;import java.io.bytearrayoutputstream;import java.io.inputstream;public class streamtools public ic byte readinputstream(inputstream instream) throws exception bytearrayoutputstream outstream = new bytearrayoutputstream(); byte buf = new by
6、te1024; int len = 0 ; while( len = instream.read(buf) != -1) outstream.(buf, 0, len); byte data = outstream.tobytearray();/网页的二进制数据 outstream.close(); instream.close(); return data; 3.编写android布局文件main.xml。 ?xml version="1.0" encoding="utf-8"? linearlayout xms:android=" andr
7、oid:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" textview android:id="+id/name" android:layout_width="100" android:layout_height="wrap_content"/ listview android:id="+id/listview" a
8、ndroid:layout_width="fill_parent" android:layout_height="wrap_content"/ /linearlayout 新建布局文件item.xml。 ?xml version="1.0" encoding="utf-8"? linearlayout xmlns:android=" android:orientation="vertical" android:layout_width="fill_parent" a
9、ndroid:layout_height="fill_parent" textview android:id="+id/name" android:layout_width="100dip" android:layout_height="wrap_content"/ textview android:id="+id/sex" android:layout_width="100dip" android:layout_height="wrap_content"
10、/ textview android:id="+id/age" android:layout_width="100dip" android:layout_height="wrap_content"/ textview android:id="+id/height" android:layout_width="100dip" android:layout_height="wrap_content"/ textview android:id="+id/is_human&
11、quot; android:layout_width="100dip" android:layout_height="wrap_content"/ textview android:id="+id/string" android:layout_width="100dip" android:layout_height="wrap_content"/ /linearlayout 在string.xml添加如下一行: string name="error" net error /s
12、tring 4.编写业务实现类。package com.shao;import java.io.inputstream;import .httpurlconnection;import .url;import java.util.arraylist;import java.util.list;import org.json.jsonarray;import org.json.jsonobject;import com.domain.contact;import com.shao.utils.streamtools;public class myservice public static lis
13、t contact getlastcontact() throws throwable string path = "00:8080/webcontent/testjson/jsondata.php" list contact contacts = new arraylist contact url url = new url(path); httpurlconnection conn = (httpurlconnection)url.openconnection(); conn.setconnecttimeout(5*1000); con
14、n.setrequestmethod("get"); inputstream instream = conn.getinputstream(); byte data = streamtools.readinputstream(instream); string json = new string(data); jsonarray array = new jsonarray(json); for(int i=0;i array.length();i+) jsonobject item = array.getjsonobject(i); contact contact = ne
15、w contact(item.getstring("name"),item.getint("age"),item.getint("sex"),item.getdouble("height"),item.getboolean("is_human"),item.getstring("string"); contacts.a(contact); return contacts; 5.编写主activity类。package com.shao;import java.util.arr
16、aylist;import java.util.hashmap;import java.util.list;import com.domain.contact;import android.app.activity;import android.os.bundle;import android.util.log;import android.widget.listview;import android.widget.simpleadapter;import android.widget.toast;public class testxml extends activity /* call wh
17、en the activity is first created. */ override public void oncreate(bundle savedinstancestate) per.oncreate(savedinstancestate); setcontentview(r.layout.main); try list contact contacts = myservice.getlastcontact(); list hashmap string,object data = new arraylist hashmap string,object (); listview li
18、stview = (listview)viewbyid(r.id.listview); for(contact contact : contacts) hashmap string,object item = new hashmap string,object item.put("name", contact.getname(); item.put("sex",contact.getsex(); item.put("age", contact.getage(); item.put("height", contact.getheight(); item.put("
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 现代家教新思路实践案例解析
- 远程教育在宠物行业人才培养中的应用
- 风险导向下企业内部财务控制的改进措施研究
- 餐饮应急预案
- 监控施工方案范文(6篇)
- 二手机械销售合同模板
- KTV装修合同执行管理制度范文
- 不锈钢建筑材料加工合同
- 交通损害赔偿合同示例
- 业务合作及分成合同书
- 《电子技术基础(第二版)》中职技工全套教学课件
- 人教版五年级上册小数乘除法竖式计算题200道及答案
- 五年级上册美术《传统门饰》课件
- DL∕T 1309-2013 大型发电机组涉网保护技术规范
- (2020版)煤矿安全生产标准化管理体系评分表
- 城乡低保待遇协议书
- DL-T5153-2014火力发电厂厂用电设计技术规程
- 华为HCIA-Storage H13-629考试练习题
- 辽宁省抚顺五十中学2024届中考化学全真模拟试卷含解析
- 2024年中国科学技术大学少年创新班数学试题真题(答案详解)
- 2024年新疆维吾尔自治区成考(专升本)大学政治考试真题含解析
评论
0/150
提交评论