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. c宏的MAX函数

    今天从香山上面回来累的跟傻逼一样,回来问了一下胡总的阿里面试的问题.然后其中有一个是宏写max函数.胡总说不好写,然后我就去洗澡了. 洗澡的时候感觉不对啊,回来写了一个: #define MAX(a, ...

  2. UVALive 4959 Jumping monkey

    题意就是: 一个猎人在森林里捕猎. 然后有只猴子,喜欢躲树的后边,猎人看不见它 然后给出了一张图,表示有哪些树是相邻的. 数据保证任意图中两个树都可以直接或间接的到达 猎人有一个枪,每次他可以选择一颗 ...

  3. Mvc中DropDownList 和DropDownListFor的常用方法

    Mvc中DropDownList 和DropDownListFor的常用方法 一.非强类型: Controller:ViewData["AreId"] = from a in rp ...

  4. Java并发编程:线程和进程的创建(转)

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  5. File类学习笔记

    File类 首先,要明确的一点就是,在整个IO包中,唯一表示与文件有关的类局势File类. 它可以实现创建或删除文件等操作.下面看看它的构造方法: File(String pathname) 通过将给 ...

  6. ASP.NET MVC 3 Model【通过一简单实例一步一步的介绍】

    今天主要讲Model的两个方面: 1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍 2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model ...

  7. [Javascript ] Array methods in depth - sort

    Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...

  8. 【转】AFNetworking之于https认证

    转自:http://www.cocoachina.com/ios/20161220/18393.html 写在开头: 本来这篇内容准备写在AFNetworking到底做了什么?(三)中的,但是因为我想 ...

  9. codesmith的使用

    新建一个C#模版. model类的模版代码如下: <%-- Name: 模型层代码生成模版 Author: XX Description: 根据数据库的内容生成模型层代码 Version: V1 ...

  10. ICSharpCode.SharpZipLib实现压缩解压缩

    最近,在项目中经常需要处理压缩和解压缩文件的操作.经过查找,发现了ICSharpCode.SharpZipLib.dll ,这是一个完全由c#编写的Zip, GZip.Tar . BZip2 类库,可 ...