因为项目中要用到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);
}
}
}
~~~
- 前言
- 安卓ListView一个简单代码的注释和探讨
- 安卓wifi热点编程代码的若干注释
- 安卓距离传感器编程
- 简单的ScrollView
- 简单的ListView
- 简单的ArrayAdapter
- AsyncTask的初步学习
- AsyncTask再度学习
- Handler初步学习
- 开启系统Activity
- 隐式调用Activity
- Activity的生命周期
- startActivityForResult的初步学习
- 多点触控拉伸图片
- LruCache图片缓存技术
- SQLiteOpenHelper数据库操作
- 短信拦截器
- 简单的Notification
- ListView优化以及checkbox状态问题
- 安卓多线程下载
- 安卓JSON解析初步探讨
- 选项卡样式的fragment
- DrawerLayout实现简单的侧滑功能
- 安卓软引用解决图片OOM问题
- 安卓隐式Intent启动Activity和BroadcastReceiver若干注意点
- Dialog学习笔记
- ActionBar使用