阅读更多 步骤一:页面返回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"?>
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:id="@+id/name"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>

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


新建布局文件item.xml。

<?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:id="@+id/name"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>
  android:id="@+id/sex"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>

  android:id="@+id/age"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>
  android:id="@+id/height"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>
  android:id="@+id/is_human"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>
  android:id="@+id/string"
  android:layout_width="100dip"
    android:layout_height="wrap_content"
/>



在string.xml添加如下一行:

net error

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 getLastContact() throws Throwable
{
  String path = "http://192.168.1.100:8080/WebContent/testjson/jsondata.php";
  List contacts = new ArrayList();
  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  {
   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 contacts = MyService.getLastContact();
   List> data = new ArrayList>();
  
   ListView  listview = (ListView)findViewById(R.id.listview);
   for(Contact contact : contacts)
   {
    HashMap item = new HashMap();
    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.开启访问网络功能 。



步骤三 测试。



















更多相关文章

  1. android横竖屏切换不重启activity解决方案
  2. Android(安卓)studio登录界面
  3. Android(安卓)Preferences的使用
  4. Android中悬浮窗口的实现原理和示例代码
  5. android的应用包名与代码包名
  6. 利用wifi连接Android真机调试React Native代码
  7. Android(安卓)使用 Socket 连接阿里云服务器
  8. Android(安卓)Studio 使用SVN check out 时报错Cannot load supp
  9. android sqllite数据库的多表联合查询

随机推荐

  1. JavaScript2020调查:Angular满意度低,Svelt
  2. OSI强调:SSPL并不是开源许可证
  3. Kubernetes在喜马拉雅的实践:测试环境稳定
  4. 树莓派发布微控制器开发板,售价仅4美元
  5. Apache ECharts顺利毕业,成为ASF顶级项目
  6. LogDevice:一种用于日志的分布式数据存储
  7. AWS 宣布创建“真正”开源的 Elasticsear
  8. 2021技术领域趋势报告:Rust继续增长、低代
  9. 解决项目迁移至Kubernetes集群中的代理问
  10. 2021红帽杯 wp(持续更新~~)