【发布时间】:2016-10-20 23:57:57
【问题描述】:
每次运行此代码时,我都会在异常中收到“找不到文件”的消息,但是,我不明白为什么当我真正将要查找的文件放在我的 c 根目录时:?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView testMEtext = (TextView) findViewById(R.id.testMEtext);
//
JSONParser parser = new JSONParser();
try {
//File temp = new File("C:/Windows/outdoorWeather.json");
//String path = temp.getAbsolutePath();
//testMEtext.setText(path);
**Object obj = parser.parse(new FileReader("/C:/outdoorWeather.json"));**
JSONObject jsonObject = (JSONObject) obj;
String oTemp = (String) jsonObject.get("Fahrenheit temperature");
testMEtext.setText(oTemp);
}
catch (FileNotFoundException e) {
e.printStackTrace();
testMEtext.setText("File not found");
}
catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}
【问题讨论】:
-
Android 应用程序无法以这种方式从您的计算机中读取文件。您需要在应用程序文件夹中添加资源。它可以在资产文件夹或 res/raw 文件夹中
标签: json android-studio