12.php做服务器生成json数据,android解析json案例简介

来源:互联网 发布:淘宝宝贝刷收藏软件 编辑:程序博客网 时间:2024/06/13 22:15

php生成json数据:使用json_encode()函数

<?php
$data1=array("id"=>"1","name"=>"cai","phone"=>"3333");
$data2=array("id"=>"1","name"=>"cai","phone"=>"4444");
$data3=array($data1,$data2);
echo json_encode($data3);
?>

android解析json数据:

package cn.caicai.json;

import java.io.ByteArrayOutputStream;
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;

public class parejson {
public static List<people> getpeople() throws Exception{
String path="http://192.168.0.117/testxml/json.php"; //远程服务器网址
URL url=new URL(path);
HttpURLConnection conn=(HttpURLConnection) url.openConnection();  //生成http连接对象
  conn.setConnectTimeout(5000); //连接过期时间
  conn.setRequestMethod("POST"); //请求方式
  if(conn.getResponseCode()==200){  //判断连接状态
  InputStream instream=conn.getInputStream(); //获取远程数据流
return paparse_joson(instream);
}
return null;

}

private static List<people> paparse_joson(InputStream instream) throws Exception{
List<people> peoples=new ArrayList<people>();
ByteArrayOutputStream outstrem=new ByteArrayOutputStream(); //生成内存字节输出流
 byte[] buffer = new byte[1024];   //每次读取1m数据
int len = 0;
while ((len = instream.read(buffer)) != -1) { //直到读取完
  outstrem.write(buffer, 0, len);  //写入内存
}
 byte[] data = outstrem.toByteArray();  //保存字节数据
 String json=new String(data); //转换为字符串
 JSONArray array=new JSONArray(json);  //生成android自带的json数组对象,此对象会自动将字符串数据解析为各个数组形式保存
for(int i=0;i<array.length();i++){
  JSONObject jsonobject=array.getJSONObject(i);  //生成json对象
people p= new people( jsonobject.getInt("id"),jsonobject.getString("name"),jsonobject.getString("phone"));
peoples.add(p);
}
return peoples;
}
}

//////////////////////////////////////////////////////////////////

package cn.caicai.json;

public class people {
int id;
String name;
String phone;

public people() {

}

@Override
public String toString() {
return "people [id=" + id + ", name=" + name + ", phone=" + phone + "]";
}

public people(int id, String name, String phone) {

this.id = id;
this.name = name;
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}

}
//////////////////////////////////////////

package cn.caicai.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Cai_josnActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listview = (ListView) this.findViewById(R.id.listview);
try {
List<people> peoples = parejson.getpeople();
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (people p : peoples) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name", p.getName());
item.put("phone", p.getPhone());
Log.i("sa", p.toString());
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this, data,
R.layout.item, new String[] { "name", "phone" }, new int[] {
R.id.name, R.id.phone });
listview.setAdapter(adapter);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

////////////////////////////////////////////////////////////////////

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

</LinearLayout>

/////////////////////////////////////////////////

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >


<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
/>
</LinearLayout>

////////////////////////////////////////////////////////////

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.caicai.json"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".Cai_josnActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

原创粉丝点击