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. HDU 4828 - Grids (Catalan数)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4828 Catalan数的公式为 C[n+1] = C[n] * (4 * n + 2) / (n ...

  2. Linq to Sql语法及实例大全

    LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的 ,而判断条件就是它后面所接的 ...

  3. 左移运算符<<

    在许多计算机编程语言(例如:C语言.C++语言.Java语言.JavaScript语言,Pascal语言等)中,“<<”代表左移运算符(就相当于'shl').该运算符为双目运算符,结合方向 ...

  4. hadoop1.X安装

    1.      配置主机的名称 master,slave1,slave2 2.      安装JDK: 3.      配置IP与主机名称的映射: 192.168.0.100 master 192.1 ...

  5. JS和利用openssl的object C加密得到相同的aes加密密文

    这是之前接到的一个工作内容,项目原本的登录操作是获得账号和密码以后,对密码进行一遍MD5加密,然后传递账号和密文到cgi文件.在c中获取到账户以后,从数据库中获取到密码,对密码进行一次MD5的加密,然 ...

  6. python代码合并

    http://www.baidu.com/s?wd=python%E4%BB%A3%E7%A0%81%E5%90%88%E5%B9%B6&rsv_bp=0&ch=&tn=mon ...

  7. <customErrors>节点说明1

    <customErrors>节点用于定义一些自定义错误信息的信息.此节点有Mode和defaultRedirect两个属性, 其中defaultRedirect属性是一个可选属性,表示应用 ...

  8. json数据相对于xml数据.

    JSON is a valid subset of JavaScript, Python, and YAML JSON parsing is generally faster than XML par ...

  9. PL/SQL中文显示都显示成“?”的问题

    PL/SQL中文显示都显示成“?”的问题  首先我在sqlplus里面查询到的中文是正常的,然后再oracle里面的注册表里面看得nls_lang是SIMPLIFIED CHINESE_CHINA.Z ...

  10. Linux samba服务器设置简单匿名共享

    linux下面的samba非常的好用,很多人拿它来作共享文件服务器, 缺省配置下,samba必须提供用户名密码来访问,如果是所有人都可以访问的内容,那么是比较麻烦的,其实通过一个设置,即可实现不用输入 ...