last day (16 days later) » 

7:41 AM
-2
Q: Getting "error: cannot find symbol method openFileOutput(String,int)" in my Android app

DJFriarI'm trying to save the results of a JSON fetch to a local file. When I try to use openFileOutput, I get error: cannot find symbol method openFileOutput(String,int). Searching said that it needs Context, but I have tried every other class I have and it either keeps the same error, or I get Non-sta...

 
Optinally you can add extra parameter for context e.g.writeToFile(Context con, String data) and do con.openFileOutput....
 
@MikeM.I realized I was mistaken in my first post. I've since edited my original question to reflect that this method is in a class extending AsyncTask. This is the JSON downloader portion of my app.
@Enzokie When I do it that way, it underlines the entire first line of that try statement and says Method call expected
 
@DJFriar if you don't mind can you post a screenshot of that Method call expected error here in the comment section? You can upload the image in Imgur and post the link here.
 
@Enzokie I posted it
 
add new keyword e.g. outputStreamWriter = new OutputStreamWriter...
 
7:41 AM
@Enzokie That fixed that part, but its complaining that when I call this method I'm not passing it the Context and nothing I've tried has made it happy. I'm tried Context, con, getContext(), and getApplicationContext() and none worked. (the method call is in the screenshot, a few lines up from the one needing the keyword new.)
 
Hi
 
This is nifty. I didn't know you could do this here.
 
Yeah :)
In the Asynctask enforce an addition constructor for Context.
Is your jsonDownload.java an asynctask?
 
public class jsonDownloader extends AsyncTask<Void,Void,String> { is my opening class declaration
 
add a public constructor that requires a context. e.g.
public jsonDownloader(Context context){ ... }
btw if you have teamviewer I can just demonstrate directly to your code.
 
7:45 AM
I think I am just an idiot. I had that and didn't even realize I had already declared context as c:
public jsonDownloader(Context c, String jsonURL, ListView lv) {
this.c = c;
this.jsonURL = jsonURL;
this.lv = lv;
}
 
oh that will be easy
just do this writeTofile(c, jsonData);
xd
 
I updated the method call and it is compiling now
 
cool
 
thank you, that seems to have fixed my issue.
 
I am glad it works :)
 
7:47 AM
now to go try to find a JSON parser tutorial I can understand.
Thank you again for all your help.
 
I use Gson for that
but for brave warrior just keep doing what you have tried so far
 
I will check out Gson, maybe it will be easier than using the built in tools.
 
yeah it is
 
cool, thank you for the hint. :)
 
I think most Android app uses Gson
 
7:50 AM
How can I mark you and the answer?
*as the answer
 
No need just leave it :D
or you can post your own answer and mark it
 
ok. thank you again
 
sure
btw if you have question you can ping me
 
I really appreciate that, thank you.,
I'm reading the Gson tutorial right now (tutorialspoint.com/gson)
 
Looks helpful :)
 
8:30 AM
DO you have a second?
 
yes
 
I am trying to use a JSON file to populate a ListView. My app correctly fetches the JSON data from a web server and saves it to a private file.
Shouldn't my adapter handle reading that JSON file and pushing it to the ListView?
 
No that's not a good design
Server Data -> App Database -> ListView
 
App Database being the local JSON file, or something else?
 
It could be any of the ff: SQLite, SharedPereference or any files
 
8:35 AM
I think it would be too much data for shared preferences (its about 2,000 entries when I'm done, its 330-ish right now)
I have it stored on a server in the JSON format, the intention is for the app to grab that file on each load, or if it can't get to it use the local one (this way the app can work without internet)
 
SQLite should be the right choice so yes you are right (Shared preference is not the ideal storage for such case)
 
so instead of trying to parse the JSON to the ListView, it would be better to go JSON to SQLite DB, and then have the ListView pull from the SQLite DB?
 
yes correct
 
ok. I will start researching that then. thank you
 
sure :)
One of the Google dev call that one as "Single source of truth"
 
8:41 AM
makes sense. probably faster using SQL then trying to parse a giant JSON
 
yeah true
 

  last day (16 days later) »