1:服务端是使用PHP,从数据库中查询出一个二维数组,然后调用系统函数以json格式返回给客户端。

返回结果如下:http://192.168.0.116/server/selectTitle2json.php

[{"title":"\u5173\u4e8e10\u67089\u65e5\u5c40\u957f\u201c12366\u5728\u7ebf\u201d\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e9\u670825\u65e5\u5c40\u957f\u201c12366\u5728\u7ebf\u201d\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e\u7f51\u4e0a\u529e\u7a0e\u7cfb\u7edf\u5c0f\u5fae\u4f01\u4e1a\u514d\u7a0e\u529f\u80fd\u5347\u7ea7\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e\u7f51\u7edc\u53d1\u7968\u7cfb\u7edf\u4e34\u65f6\u6682\u505c\u7684\u7d27\u6025\u901a\u77e5"},{"title":"\u5173\u4e8e9\u67083\u65e5\u4e92\u8054\u7f51\u201c\u5728\u7ebf\u8bbf\u8c08\u201d\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e9\u67084\u65e5\u5c40\u957f\u201c12366\u5728\u7ebf\u201d\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e\u542f\u7528\u8d27\u7269\u8fd0\u8f93\u4e1a\u589e\u503c\u7a0e\u4e13\u7528\u53d1\u7968\u7f51\u4e0a\u6284\u62a5\uff08\u542b\u673a\u52a8\u8f66\uff09\u7cfb\u7edf\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e\u7f51\u4e0a\u529e\u7a0e\u7cfb\u7edf\u81f4\u8425\u6539\u589e\u7eb3\u7a0e\u4eba\u7684\u4e00\u5c01\u4fe1"},{"title":"\u5173\u4e8e\u7f51\u4e0a\u529e\u7a0e\u7cfb\u7edf\u8425\u6539\u589e\u529f\u80fd\u5347\u7ea7\u7684\u901a\u77e5"},{"title":"\u5173\u4e8e8\u670828\u65e5\u5c40\u957f\u201c12366\u5728\u7ebf\u201d\u7684\u901a\u77e5"}]

2:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btn_get_titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Titles"/> <ListView
android:id="@+id/lv_show"
android:layout_below="@id/btn_get_titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

3:MainActivity.java

public class MainActivity extends Activity implements OnClickListener {
private Button btnGetTitles=null;
private ListView lvShow=null;
private List<Object> titleList=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initUI(); btnGetTitles.setOnClickListener(this);
} private void initUI(){
btnGetTitles=(Button)findViewById(R.id.btn_get_titles);
lvShow=(ListView)findViewById(R.id.lv_show);
} @Override
public void onClick(View arg0) {
new Thread(new GetTitlesThread()).start();
} Handler getTitlesHandler=new Handler(){
public void handleMessage(Message msg){
if(msg.what==100){
ArrayAdapter<Object> adapter=new ArrayAdapter<Object>(
MainActivity.this,
android.R.layout.simple_list_item_1,
titleList);
lvShow.setAdapter(adapter);
}
}
};
class GetTitlesThread implements Runnable{
@Override
public void run() {
String url="http://www.zhihuiqd.com/wsht/server/selectTitle2json.php";
StringBuilder builder=new StringBuilder();
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet(url);
try{
HttpResponse response=client.execute(get); if(response.getStatusLine().getStatusCode()==200){
BufferedReader reader=new BufferedReader(
new InputStreamReader(response.getEntity().getContent())); for(String s=reader.readLine();s!=null;s=reader.readLine()){
builder.append(s);
} JSONArray json=new JSONArray(builder.toString());
int len=json.length();
String title="";
titleList=new ArrayList<Object>(); for(int i=0;i<len;i++){
JSONObject temp=(JSONObject)json.get(i);
title=temp.getString("title");
titleList.add(title);
} getTitlesHandler.obtainMessage(100).sendToTarget();
}
}catch(Exception e){
//
}
}
}
}

4:最后不要忘记在AndroidManifest.xml文件中加入:

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

Android 解析JSON数组的更多相关文章

  1. 使用QtScript库解析Json数组例子

    本文转载自:http://blog.sina.com.cn/s/blog_671732440100uwxh.html 使用qtscipt库解析json数组首先在工程文件中加 QT        += ...

  2. 解析json数组

    解析json数组 JSONArray jsonArray = new JSONArray(markingStr); int iSize = jsonArray.length(); for (int i ...

  3. Gson解析Json数组

    需求:从steam官网获取英雄数据,即为Json数据,并导入到本地数据库 Json数据是这样的 { "result": { "heroes": [ { &quo ...

  4. Jquery解析json数组字符串

    最近在工作中用到了Jquery来解析json字符串,网上解析jquery解析json单个对象的实例不少,但是jquery解析json数组的实例却是不多,下面我举一个简单的例子来跟大家分享与一下,本人水 ...

  5. java解析json数组

      java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...

  6. hive中解析json数组

    -- hive中解析json数组 select t1.status ,substr(ss.col,,) as col ,t3.evcId ,t3.evcLicense ,t3.evcAddress , ...

  7. fastjson解析json数组

    1.fastjson解析json数组(直接上代码) import java.util.ArrayList; import java.util.List; import com.alibaba.fast ...

  8. 前端学习之——js解析json数组

    ** 前端学习之——js解析json数组** 解析json数组即对JSONArray的遍历 一.对于标准的json数组如: var result=[{"flag":1," ...

  9. C#解析JSON数组

    方式一 第一步:使用前,需下载:Newtonsoft.Json.dll 没有的,请到我百度云盘下载 链接:https://pan.baidu.com/s/1JBkee4qhtW7XOyYFiGOL2Q ...

随机推荐

  1. Selenium webdriver 查找元素

    1.简单查找 By ID: WebElement element=driver.findElement(By.id("userId")); By Name:WebElement e ...

  2. jQuery Ajax全解析

    jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 我们先来看一些简单的方法,这些方法都是对jQuery.ajax( ...

  3. ganglia单播配置

    背景:    有时,由于当前网络不支持组播等种种原因,使用gmond默认的配置gmetad不能获取到各个客户端的全部数据,http://x.x.x.x/ganglia页面一个cluster组只能展示一 ...

  4. 标C编程笔记day06 动态分配内存、函数指针、可变长度參数

    动态分配内存:头文件 stdlib.h     malloc:分配内存     calloc:分配内存,并清零     realloc:调整已分配的内存块大小     演示样例:         in ...

  5. javascript內容向上不間斷滾動

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 基础-ADO插入数据后返回自增ID @@IDENTITY

    在文件上传中,没上传一个文件都会插入一条数据信息,那么就要返回插入的数据的id,以便进行真实删除操作.以下是ADO操作数据库的返回方法: string sql = string.Format(@&qu ...

  7. Resizing LVM Logical Volumes-lvextend

    1. fdisk命令/dev/sdc再分出一个sdc2分区 [root@rhel7 ~]# fdisk /dev/sdc Welcome to fdisk (util-linux ). Changes ...

  8. jdbc01

    1.创建对应的数据库以及表 /* SQLyog 企业版 - MySQL GUI v8.14 MySQL - 5.5.32-log : Database - news ***************** ...

  9. web页面打印

    在使用的两种方式打印: 第一种:js如下 function doPrint() { allhtml = window.document.body.innerHTML; starstr = " ...

  10. 利用反射的特性将DataReader对象转化为List集合

    问题:将SqlDataReader对象转换为List<T>集合 思路: 1,利用反射的特性得到对应实体Model的公共属性 Type type = typeof(T); PropertyI ...