Android 读取app内json配置文件
来源:互联网 发布:java中成员方法参数 编辑:程序博客网 时间:2024/05/03 10:25
Android 读取app内json配置文件
1 Json配置文件放置于Android 项目assets 文件下
Android除了提供/res目录存放资源文件外,在/assets目录也会提供存放资源文件,在/assets目录下面不会在R.java里面自动生 成ID,所以读取assets目录下面的资源文件需要提供路径,我们可以通过AssetManager类来访问这些文件。
2 在Activity中定义文件名称
private final static String fileName = "mainlistitems.json";//创建Thread 读取文件class DataThread extends Thread {@Overridepublic void run() {String jsonStr = AppJsonFileReader.getJson(getBaseContext(),fileName);data = AppJsonFileReader.setListData(jsonStr);dataHandler.sendMessage(dataHandler.obtainMessage());}}
3 AppJsonFileReader 类使用 AssetManager 读取文件如下,读取文本并解析存入Map中
package com.jason.listviewoverview.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.content.Context;import android.content.res.AssetManager;public class AppJsonFileReader {public static String getJson(Context context, String fileName) {StringBuilder stringBuilder = new StringBuilder();try {AssetManager assetManager = context.getAssets();BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));String line;while ((line = bf.readLine()) != null) {stringBuilder.append(line);}} catch (IOException e) {e.printStackTrace();}return stringBuilder.toString();}public static List<Map<String, String>> setData(String str) {try {List<Map<String, String>> data = new ArrayList<Map<String, String>>();JSONArray array = new JSONArray(str);int len = array.length();Map<String, String> map;for (int i = 0; i < len; i++) {JSONObject object = array.getJSONObject(i);map = new HashMap<String, String>();map.put("operator", object.getString("operator"));map.put("loginDate", object.getString("loginDate"));map.put("logoutDate", object.getString("logoutDate"));data.add(map);}return data;} catch (JSONException e) {e.printStackTrace();return null;}}public static List<Map<String, String>> setListData(String str) {List<Map<String, String>> data = new ArrayList<Map<String, String>>();try {JSONArray array = new JSONArray(str);int len = array.length();Map<String, String> map;for (int i = 0; i < len; i++) {JSONObject object = array.getJSONObject(i);map = new HashMap<String, String>();map.put("imageId", object.getString("imageId"));map.put("title", object.getString("title"));map.put("subTitle", object.getString("subTitle"));map.put("type", object.getString("type"));data.add(map);}} catch (JSONException e) {e.printStackTrace();}return data;}}
4 code 见
https://github.com/jingshauizh/androidsummary/tree/master/ListViewOverView
3 0
- Android 读取app内json配置文件
- Unity读取 JSon配置文件
- Unity读取 JSon配置文件
- 读取Json配置文件问题
- winform读取配置文件App.config
- Android 读取Properties配置文件
- android读取properties配置文件
- android读取properties配置文件
- android读取配置文件写法
- android读取properties配置文件
- 记录下vs2008 读取 App.config 配置文件
- winform读取app.config.配置文件
- WinForm 读取配置文件(app.ini)
- c#读取配置文件 程序的App.config
- C# 读取保存App.config配置文件
- C# winForm读取配置文件 App.config
- C#读取app.settings配置文件连接数据库
- Extjs6.2 打包配置文件(app.json)
- Win7硬盘安装Ubuntu变成双系统
- Unity Shader 基础知识(一)
- ASP.NET MVC Jquery Validate 表单验证的多种方式
- 关于纯代码适配SizeClass
- 第一章 Hello Purr(1/2)
- Android 读取app内json配置文件
- 驱动的动态加载与静态加载
- mysql数据库,多表查询
- Ubuntu安装后grub无法正确引导进入Win7系统的两种有效解决方法
- 深入分析 Java I/O 的工作机制
- wordpress文章缩略图的使用
- Ubuntu基本桌面环境
- spring常用注解
- MPI模拟文件读取及任务分发