从Java代码Android访问res/raw文件夹中的json文件
我在试图找出如何从Android项目的资产文件夹中访问quick.JSON文件时遇到了一些问题从Java代码Android访问res/raw文件夹中的json文件,java,android,json,Java,Android,Json,我在试图找出如何从Android项目的资产文件夹中访问quick.JSON文件时遇到了一些问题 InputStream is = context.getAssets().open("departments.json"); 这是我用来从JSON文件加载问题的方法: public ArrayList<Question> loadQuestions() throws IOException, JSONException { ArrayList<Question> qu
InputStream is = context.getAssets().open("departments.json");
这是我用来从JSON文件加载问题的方法:
public ArrayList<Question> loadQuestions() throws IOException, JSONException {
ArrayList<Question> questions = new ArrayList<Question>();
BufferedReader reader = null;
try{
// Open and read the file into a StringBuilder
//InputStream in = mContext.openFileInput(mContext.getResources().getString(R.raw.quiz));
//InputStream in = mContext.getResources().openRawResource(R.raw.quiz);
InputStream in = mContext.getAssets().open("quiz.js");
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder jsonString = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// Line breaks are omitted and irrelevant
jsonString.append(line);
}
// Parse the JSON using JSONTokener
JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
// Build the array of crimes from JSONObjects
for(int i=0; i< array.length(); i++){
questions.add(new Question(array.getJSONObject(i)));
}
}
catch (FileNotFoundException e){
// Ignore this one; it happens when starting fresh
}
finally{
if(reader != null){
reader.close();
}
}
return questions;
}
InputStream is = context.getAssets().open("departments.json");
]
InputStream is = context.getAssets().open("departments.json");
这是我尝试使用JSON中的数据的活动
public class QuizActivity extends Activity {
private ArrayList<Question> questions;
private QuizJSONSerializer mSerializer;
private static final String TAG = "QuizActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
// This is where i get the loadQuestions from
mSerializer = new QuizJSONSerializer(getApplicationContext());
try{
questions = mSerializer.loadQuestions();
}
catch (Exception e){
questions = new ArrayList<Question>();
Log.e(TAG, "Error loading questions: ", e);
}
ImageView iv = (ImageView) findViewById(R.id.question_image);
iv.setImageResource(R.drawable.mdpi_farlig_sving);
TextView tv = (TextView) findViewById(R.id.question_text_view);
if(questions == null){
tv.setText("Something wrong with the array!");
}
else{
tv.setText(questions.get(0).getQuestion());
}
Button b1 = (Button) findViewById(R.id.option_one);
b1.setText("String 1");
Button b2 = (Button) findViewById(R.id.option_two);
b2.setText("String 2");
Button b3 = (Button) findViewById(R.id.option_three);
b3.setText("String 3");
Button b4 = (Button) findViewById(R.id.option_four);
b4.setText("String 4");
}
InputStream is = context.getAssets().open("departments.json");
}
InputStream is = context.getAssets().open("departments.json");
似乎JSON文件从未加载,因为我的异常中的代码始终运行
InputStream is = context.getAssets().open("departments.json");
JSON文件中是否缺少某些内容
InputStream is = context.getAssets().open("departments.json");
有什么好建议吗?您应该创建资产文件夹并将文件放在那里。然后你可以用它来访问你的文件
InputStream is = context.getAssets().open("departments.json");
为什么不使用assets文件夹将JSON捆绑到应用程序中?它毕竟是一种资产,而不是原始资源。使用以下方法创建资产文件夹:
InputStream is = context.getAssets().open("departments.json");
InputStream input = context.getAssets().open("yourJSON.json");
您将访问您的文件。mContext.getResources.openrawsourcer.raw.quick应该可以。。。IndexOutOfBounds未与此代码连接…IndexOutOfBounds?你确定它与这段代码有关吗?实际上,现在我不确定了,因为现在我把JSON文件移到了assets文件夹,并在下面的帖子中尝试了Ragaisis解决方案。这也不起作用。getAssets和res/raw不是一回事。。。
InputStream is = context.getAssets().open("departments.json");