android取得json数据

巴扎黑
发布: 2016-11-24 11:06:10
原创
1132人浏览过

步骤一:页面返回json数据。 

对本空间日志“php和ajax的简单实现”的json.php代码做一些简单修改,代码如下:将代码 echo $_get['jsoncallback'].'('.custom_json::encode($big_test).')'; 

改为 echo $_get['jsoncallback'].custom_json::encode($big_test); 

将文件另存为jsondata.php。 

步骤二:编写android代码。 

1.编写contact类,设置set和get方法。 

package com.domain; 

public class contact { 
private string name; 
private int age; 
private int sex; 
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) { 
  this.name = name; 
  this.age = age; 
  this.sex = sex; 
  this.height = height; 
  this.is_human = is_human; 
  this.string = string; 

public string getname() { 
return name; 


public void setname(string name) { 
this.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.sex = 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.string = string; 




2.编写输入流处理工具类streamtools。 

package com.shao.utils; 

import java.io.bytearrayoutputstream; 
import java.io.inputstream; 

public class streamtools { 
public static byte[] readinputstream(inputstream instream) throws exception{ 
  bytearrayoutputstream outstream = new bytearrayoutputstream(); 
  byte[] buf = new byte[1024]; 
  int len = 0 ; 
  while(( len = instream.read(buf)) != -1) 
  { 
   outstream.write(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 xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <textview 
android:id="@+id/name" 
  android:layout_width="100dip" 
    android:layout_height="wrap_content" 
/> 

<listview 
android:id="@+id/listview" 
  android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 
</linearlayout> 

新建布局文件item.xml。 

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android: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" 
/> 

  <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" 
  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</string> 

4.编写业务实现类。 

package com.shao; 

import java.io.inputstream; 
import java.net.httpurlconnection; 
import java.net.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 list<contact> getlastcontact() throws throwable 

  string path = "http://192.168.1.100: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); 
  conn.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 = new contact(item.getstring("name"),item.getint("age"),item.getint("sex"),item.getdouble("height"),item.getboolean("is_human"),item.getstring("string"));
   contacts.add(contact); 
  } 
  
  return contacts; 





5.编写主activity类。 

package com.shao; 

import java.util.arraylist; 
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 { 
    /** called when the activity is first created. */ 
    @override 
    public void oncreate(bundle savedinstancestate) { 
        super.oncreate(savedinstancestate); 
        setcontentview(r.layout.main); 
        try { 
   list<contact> contacts = myservice.getlastcontact(); 
   list<hashmap<string,object>> data = new arraylist<hashmap<string,object>>(); 
   
   listview  listview = (listview)findviewbyid(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("is_human", contact.isis_human()); 
    item.put("string", contact.getstring()); 
    data.add(item); 
    
   } 
   
   
   
   
   
   simpleadapter adapter = new simpleadapter(this, data, r.layout.item, 
     new string[]{"name","sex","age","height","is_human","string"}, 
     new int[]{r.id.name, r.id.sex, r.id.age, r.id.height, r.id.is_human, r.id.string}); 
   
   listview.setadapter(adapter); 
   
   
   
  } catch (throwable e) { 
   log.e("shao",e.tostring()); 
   toast.maketext(this, r.string.error, 1).show(); 
  } 
    } 




6.开启访问网络功能 。 

<uses-permission android:name="android.permission.internet"/> 

步骤三 测试。 

Find JSON Path Online
Find JSON Path Online

Easily find JSON paths within JSON objects using our intuitive Json Path Finder

Find JSON Path Online 30
查看详情 Find JSON Path Online
相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号