安卓JSON解析初步探讨

最后更新于:2022-04-01 14:43:39

因为项目中要用到JSON解析,所以就学习了一下。有关于安卓JSON的教程网上有很多,来来去去都是这几个类的介绍。其实这一篇文章甚至也不能算是一篇学习心得,只能理解为一份备忘录。 下面是我要解析的JSON格式: ~~~ [{"file_id": "13", "filename": "grub2\u6559\u7a0b.pdf"}, {"file_id": "14", "filename": "demo.pdf"}] ~~~ 这里面要用JSONArray来存储数组,然后用JSONObject获得里面的每一个键值对。 因为是随便写写,所以不免很多纰漏,listview的优化也没写。代码纯当笑话。 ~~~ package com.example.getpdflist; import java.io.InputStream; import java.util.ArrayList; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; /** * 该类从服务器获取PDF文件名称和ID关系,获取方式:JSON * * @author USER * */ public class MainActivity extends Activity { // 网页传递JSON信息路径 private final String JSON_PATH = "http://sysucs.org:8080/list"; // 获得的JSON字符串内容 private StringBuilder str; // 从JSON获取的名字与ID对应关系 private ArrayList<Pair> jsonContent; private ListView lv; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { showListView(); }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); jsonContent = new ArrayList<Pair>(); str = new StringBuilder(); new Thread(new getPdfThread()).start(); } /** * 该函数将stringBuilder转化为map对应关系 */ private void handleJson() { try { JSONArray jsonArray = new JSONArray(str.toString()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("filename"); int id = jsonObject.getInt("file_id"); Pair pair = new Pair(name, id); jsonContent.add(pair); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 为list设置内容 private void showListView() { lv.setAdapter(new myAdapter()); } private class myAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return jsonContent.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Pair pair = jsonContent.get(position); System.out.println(pair.id + " " + pair.name); convertView = View.inflate(MainActivity.this, R.layout.item, null); TextView name = (TextView) convertView.findViewById(R.id.file_name); TextView id = (TextView) convertView.findViewById(R.id.file_id); name.setText(pair.name); id.setText(String.valueOf(pair.id)); return convertView; } } /** * 存储name id对的类 * * @author Hehyu * */ private class Pair { public String name; public int id; public Pair(String name, int id) { this.name = name; this.id = id; } } public class getPdfThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(JSON_PATH); try { HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); InputStream is = httpEntity.getContent(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != 0) { String t = new String(buffer, 0, len); str.append(t); } } catch (Exception e) { e.printStackTrace(); } // 获取JSON对应关系 handleJson(); Message msg = Message.obtain(); handler.sendMessage(msg); } } } ~~~
';