I've tried many ways but they all keep saying the file doesn't exist, but it does. It's located in the java
folder but when it tries to read it it fails. I've taken out the JSON codes, and storing it in to Cloudboost works when I hard code it, but i can't do it for over 1000 questions and answers...
This is the updated code:
public String loadJSON() {
String json = null;
try {
InputStream is = this.getAssets().open("QA.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;
}
public void addToTable() throws CloudException, FileNotFoundException, IOException, ParseException {
CloudObject table = new CloudObject("QuestionsAndAnswers");
ArrayList<String> question = new ArrayList<String>();
ArrayList<String> answer = new ArrayList<String>();
ArrayList<Double> earnings = new ArrayList<Double>();
JSONParser jsonParser = new JSONParser();
Object obj = jsonParser.parse( new FileReader( loadJSON() ) );
JSONArray jsonArray = (JSONArray) obj;
for (int i = 0 ; i < jsonArray.size() ; i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
question.add( (String) jsonObject.get("Question") );
answer.add( (String) jsonObject.get("Answer") );
if ( jsonObject.get("Earnings") != null ) {
earnings.add( (Double) jsonObject.get("Earnings") );
}
}
question.add("Hello" + "?");
answer.add("Hi");
earnings.add(100.00);
question.add("What's up" + "?");
answer.add("Nothing.");
earnings.add(50.00);
for (int i = 0 ; i < question.size() ; i++) {
table.set("question", question.get(i));
table.set("answer", answer.get(i));
table.set("earnings", earnings.get(i));
table.save(new CloudObjectCallback() {
@Override
public void done(CloudObject x, CloudException e) {
if (e != null) {
// Error
Log.i("TABLE ERROR:", e.getMessage());
}
if (x != null) {
// CloudObject
}
}
});
}
}
This is the error I use to get:
03-08 15:38:31.422 7817-7843/? W/System.err: java.io.FileNotFoundException: QA.json: open failed: ENOENT (No such file or directory)
03-08 15:38:31.423 7817-7843/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
03-08 15:38:31.423 3182-5526/? W/ActivityManager: getRunningAppProcesses: caller 10136 does not hold REAL_GET_TASKS; limiting output
03-08 15:38:31.423 7817-7843/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
03-08 15:38:31.423 7817-7843/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:103)
03-08 15:38:31.423 7817-7843/? W/System.err: at java.io.FileReader.<init>(FileReader.java:66)
03-08 15:38:31.423 7817-7843/? W/System.err: at com.ehlien.clevercash.WelcomeActivity.addToTable(WelcomeActivity.java:75)
03-08 15:38:31.423 7817-7843/? W/System.err: at com.ehlien.clevercash.WelcomeActivity$AddToTable.doInBackground(WelcomeActivity.java:119)
03-08 15:38:31.423 7817-7843/? W/System.err: at com.ehlien.clevercash.WelcomeActivity$AddToTable.doInBackground(WelcomeActivity.java:115)
03-08 15:38:31.423 7817-7843/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
03-08 15:38:31.423 7817-7843/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-08 15:38:31.423 7817-7843/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-08 15:38:31.423 7817-7843/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-08 15:38:31.423 3182-5826/? W/ActivityManager: getRunningAppProcesses: caller 10136 does not hold REAL_GET_TASKS; limiting output
03-08 15:38:31.423 7817-7843/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-08 15:38:31.423 7817-7843/? W/System.err: at java.lang.Thread.run(Thread.java:818)
03-08 15:38:31.423 7817-7843/? W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
03-08 15:38:31.428 458-1078/? E/NetlinkEvent: NetlinkEvent::FindParam(): Parameter 'ALERT_NAME' not found
03-08 15:38:31.428 7817-7843/? W/System.err: at libcore.io.Posix.open(Native Method)
03-08 15:38:31.429 7817-7843/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
03-08 15:38:31.429 7817-7843/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
03-08 15:38:31.429 7817-7843/? W/System.err: ... 12 more
This is the error I now get:
03-08 16:01:58.947 6762-18889/? D/PackageBroadcastService: Received broadcast action=android.intent.action.PACKAGE_REPLACED and uri=com.ehlien.clevercash
03-08 16:01:58.957 18865-18890/com.ehlien.clevercash W/System.err: java.io.FileNotFoundException: [
03-08 16:01:58.957 18865-18890/com.ehlien.clevercash W/System.err: {
03-08 16:01:58.957 18865-18890/com.ehlien.clevercash W/System.err: "Question":"Carl and the Passions changed band name to what",
03-08 16:01:58.957 18865-18890/com.ehlien.clevercash W/System.err: "Answer":"Beach Boys"
03-08 16:01:58.957 18865-18890/com.ehlien.clevercash W/System.err: },
(All the way to the end of the file)...
03-08 16:01:58.979 18865-18890/com.ehlien.clevercash W/System.err: ]: open failed: ENAMETOOLONG (File name too long)
03-08 16:01:58.979 18865-18865/com.ehlien.clevercash D/AppTracker: App Event: start
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:103)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.io.FileReader.<init>(FileReader.java:66)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at com.ehlien.clevercash.WelcomeActivity.addToTable(WelcomeActivity.java:75)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at com.ehlien.clevercash.WelcomeActivity$AddToTable.doInBackground(WelcomeActivity.java:119)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at com.ehlien.clevercash.WelcomeActivity$AddToTable.doInBackground(WelcomeActivity.java:115)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: at java.lang.Thread.run(Thread.java:818)
03-08 16:01:58.980 18865-18890/com.ehlien.clevercash W/System.err: Caused by: android.system.ErrnoException: open failed: ENAMETOOLONG (File name too long)
03-08 16:01:58.981 6762-18858/? D/k: Processing package: com.ehlien.clevercash
It's directly under the app/java folder
That is on your development machine. That is not on the Android device.
If you want to ship JSON with your Android app, put it in assets/
. Then, use AssetManager
to get an InputStream
on the asset to pass to your desired JSON parser.