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. maven下载jta失败,自己本地安装jta库

    mvn install:install-file -Dfile=./jta-1_0_1B-classes.zip -DgroupId=javax.transaction -DartifactId=jt ...

  2. 合泰 HT66F30 定时器初始化

    #define TM0_RUN (0<<7) //TM0运行 #define TM0_PAUST (1<<7) //TM0暂停 #define TM0_DIV4 (0<& ...

  3. IOS实现小型计算器

    作为一名初学者,编辑一款能够在IOS操作系统上运行的计算器是一件很值得自豪的事情,网络上虽然后很多相关的文章和代码,功能也很强大但是我感觉相关的计算器比加复杂,晦涩难懂,所以我想通过这个小小的计算器, ...

  4. 通过linux ssh远程登录另一台Linux,无需密码,用证书验证

    1.首先登入一台linux服务器,此台做为母机(即登入其他linux系统用这台做为入口):执行一行命令生成key文件:ssh-keygen -t rsa 2.在母机上,进入/roo/.ssh目录,找到 ...

  5. 适配ios7

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) { self.edgesForExtendedLayout = UIR ...

  6. NSRunLoop(来自官方文档)

    The NSRunLoop class declares the programmatic interface to objects that manage input sources. An NSR ...

  7. Java基础知识强化31:String类之String的面试题

    1.先看一个图: 2.String面试题: (1)题1: package cn.itcast_02; /* * 看程序写结果 */ public class StringDemo3 { public ...

  8. 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

    [题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...

  9. UVA 11770 Lighting Away

    RunID User Problem Result Memory Time Language Length Submit Time 2482977 zhyfzy J Accepted 0 KB 138 ...

  10. android常用http框架介绍

    测试数据 1.HttpURLConnection:在Android 2.2版本之前,HttpClient拥有较少的bug,因此使用它是最好的选择.而在Android 2.3版本及以后,HttpURLC ...