0

Every time I run this code, I get the message "File not found" in my exception however, I don't understand why is that when I literally have the file I'm looking for sitting at the root of my 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 application cannot read file from your computer in this way. You need to add the resource in your application folder. It can be in asset folder or res/raw folder – Paulo Oct 21 '16 at 00:00

1 Answers1

0

I don't think you can interact with objects outside of your emulator. To solve it try referring to these.
Load a simple text file in android studio
How can I read a text file in android
Reading a simple text file

Community
  • 1
  • 1
Nicky HFE
  • 143
  • 9