- 浏览: 21401 次
- 性别:
- 来自: 北京
文章分类
最新评论
步骤一:页面返回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"/>
步骤三 测试。
对本空间日志“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"/>
步骤三 测试。
发表评论
-
android win8效果实现进阶(一)
2013-10-24 23:30 899最近在研究android 版本的win8效果,只牵强实现了一部 ... -
AsyncImageLoader
2013-07-31 21:37 0非原著,节选自http://code.google.com/p ... -
ubuntu 12.04驱动开发环境配置
2013-05-04 00:46 21011.在终端运行#uname-r查看现有的内核的版本,本人ubu ... -
android取得收藏夹的联系人
2012-06-06 18:43 1765摘自:http://blog.csdn.net/qiaonin ... -
Android 删除手机联系人,添加手机联系人,更新手机联系人信息
2012-05-21 21:25 2967添加联系人 private void addContact ... -
android源码下载以及编译
2012-02-25 23:23 1648redhat linux 一、下载并编译安装git wge ... -
android底层开发
2012-02-24 16:31 994·Android开发:如何实现TCP和UDP传输 http:/ ... -
android adb shell常用命令
2012-02-20 18:20 6872最进学到了adb工具的一些小技巧,记录在此。操作系统是wind ...
相关推荐
eclipse项目。获取raw文件下的json文件。无需编写json数据里面key值的实体类,动态获取里面的键值对的值。并在列表显示
最近在整理自己写过的一些应用,发现这个也许对大家有帮助,android通过http页面获取json标准格式数据并且解析其中对象的全过程,其中包含android连接http页面方法,android解析json格式数据方法,json标准化格式...
2. 服务器端提供http(get)获取方式获取json数据的api例子,是用php写的。 4. android的apk给你说完成http数据请求和json获取解析。 5. 把index.php部署到你的php服务器. 6. 修改MainActivity.java中的 final ...
android,获取json数据,根据URL获取图片,添加图片到listview,烧流量
首先客户端从服务器端获取json数据 1、利用HttpUrlConnection 代码如下:/** * 从指定的URL中获取数组 * @param urlPath * @return * @throws Exception */ public static String readParse(String urlPath) ...
android httpclient 访问服务器 获取json数据,里面有需要载入的外部jar包,添加源代码后再加入外部包就可。
在模块对应的包名下创建JSON数据对应的实体类WeatherInfo类,实体类中的成员名称要与JSON数组的key值一致,并为每个属性添加get和set方法。public String getTemp() {return temp;}public void setTemp(String temp)...
Android例子源码httpclient获取到JSON数据并展示到ListView
android客户端从服务器端获取json数据并解析
android客户端通过volley框架获取json数据,并显示在文本中,测试可行,放心下载!!
模似获取JSON数据,显示计步器的个人信息,POST数据,获取JOSN字符后解析。
http连接 获取json 解析json 显示json数据 Springboot服务器
Android获取从服务器接口获取Json,App数据上传服务器。
利用金山词霸开放性api,通过异步处理类访问网络 获取到json,再通过Gson解析json,获取到金山词霸 的每日一句数据.
Android HTTP网络请求获取JSON数据
本demo实现了通过wifi获取服务器端的json数据,(服务器端是servlet)。
android中post获取json,你懂得,还是比较详细的
Android高级应用源码-通过httpclient获取到JSON数据,展示到ListView.zip