Android 解析JSON数组
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"/>