您好,欢迎访问一九零五行业门户网

android取得json数据

步骤一:页面返回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 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"/>
步骤三 测试。
其它类似信息

推荐信息