Android 解析assets目錄下的json檔案
阿新 • • 發佈:2019-01-09
Context test_Context = null;
String test_package = packageName;
try
{
test_Context = context.createPackageContext(
test_package, Context.CONTEXT_IGNORE_SECURITY);
AssetManager s = test_Context.getAssets();
try{
InputStream is = s.open("info.json");
byte [] buffer = new byte[is.available()] ;
is.read(buffer);
String json = new String(buffer,"utf-8");
is.close();
JSONObject obj;
try {
obj = new JSONObject(json);
String title = obj.getString("name");
if(title.isEmpty() == false){
return title;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
String test_package = packageName;
try
{
test_Context = context.createPackageContext(
test_package, Context.CONTEXT_IGNORE_SECURITY);
AssetManager s = test_Context.getAssets();
try{
InputStream is = s.open("info.json");
byte [] buffer = new byte[is.available()] ;
is.read(buffer);
String json = new String(buffer,"utf-8");
is.close();
JSONObject obj;
try {
obj = new JSONObject(json);
String title = obj.getString("name");
if(title.isEmpty() == false){
return title;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}