我正在开发和Android应用程序,其中我不想读取本地json文件来填充ListView,问题是我无法读取本地json文件,这里我如何继续。
首先我创建了一个对象类,因为我想使用 ObjectMapper 。
已经创建了基类(Theme,CouchesTheme和ClassesEvenement)。
这是子类 LireTheme :
package com.myapp.theme;
public class LireTheme {
private Theme theme;
private CouchesTheme couchesTheme;
private ClassesEvenement classesEvenement;
private String message;
private boolean ok;
public LireTheme() {
super();
// TODO Auto-generated constructor stub
}
public LireTheme(Theme theme, CouchesTheme couchesTheme,
ClassesEvenement classesEvenement, String message, boolean ok) {
super();
this.theme = theme;
this.couchesTheme = couchesTheme;
this.classesEvenement = classesEvenement;
this.message = message;
this.ok = ok;
}
public Theme getTheme() {
return theme;
}
public void setTheme(Theme theme) {
this.theme = theme;
}
public CouchesTheme getCouchesTheme() {
return couchesTheme;
}
public void setCouchesTheme(CouchesTheme couchesTheme) {
this.couchesTheme = couchesTheme;
}
public ClassesEvenement getClassesEvenement() {
return classesEvenement;
}
public void setClassesEvenement(ClassesEvenement classesEvenement) {
this.classesEvenement = classesEvenement;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isOk() {
return ok;
}
public void setOk(boolean ok) {
this.ok = ok;
}
}
第二我创建了一个主题列表的对应HashMap类。
package com.myapp.recensement;
import java.util.ArrayList;
import java.util.HashMap;
import com.myapp.theme.LireTheme;
public class LireThemes extends HashMap<String, ArrayList<LireTheme>> {
private static final long serialVersionUID = 1L;
}
然后像这样使用它。
package com.myapp.recensement;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import android.os.Environment;
import android.util.Log;
import com.myapp.theme.LireTheme;
public class LireThemesController {
private ObjectMapper objectMapper = null;
private JsonFactory jsonFactory = null;
private JsonParser jp = null;
private ArrayList<LireTheme> themeList = null;
private LireThemes lirethemes = null;
private File jsonFile;
boolean availble=false;
public LireThemesController() {
objectMapper = new ObjectMapper();
jsonFactory = new JsonFactory();
}
public void init() {
availble=isExternalStorageReadble();
if(availble){
Log.w("myApp", "file available");
}else{
Log.w("myApp", "file not available");
}
jsonFile = new File("c:\\lireThemes.json");
try{
jp = jsonFactory.createJsonParser(jsonFile);
lirethemes = objectMapper.readValue(jp, LireThemes.class);
themeList = lirethemes.get("themes");
} catch(JsonParseException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
public ArrayList<LireTheme> findAll() {
return themeList;
}
public LireTheme findById(int id) {
return themeList.get(id);
}
public boolean isExternalStorageReadble() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
}
并且还已在AndroidManifest中添加了权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
当我跑步时我得到这个错误:
12-29 04:16:04.287: W/System.err(2060): java.io.FileNotFoundException: /c:\lireThemes.json: open failed: ENOENT (No such file or directory).
请问这个问题的任何解决方案吗?
答案 0 :(得分:1)
您无法从c:\读取Android设备上的文件,这是一个Windows文件系统路径。
您必须在Assets文件夹中复制JSON文件,然后使用
加载它public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("yourfilename.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
答案 1 :(得分:0)
问题是jsonFile = new File("c:\\lireThemes.json");
你的Android设备上没有c:\ lireThemes.json文件。
如果您将文件添加到资产目录(在项目中),您可以通过getAssets().open("lireThemes.json")
访问,这将返回输入流
或者您可以将它放在res / raw文件夹中并通过getActivity().getResources().openRawResource(R.raw.lireThemes)
访问它,这也将返回一个输入流