1

我尝试了很多方法,但他们都一直说该文件不存在,但确实存在。它位于java文件夹中,但是当它尝试读取它时失败。我已经取出了 JSON 代码,并将其存储到 Cloudboost 中,当我对其进行硬编码时,它可以工作,但是对于超过 1000 个问题和答案,我无法做到这一点......

这是更新的代码:

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
                }
            }
        });
    }
}

这是我用来获取的错误:

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

这是我现在得到的错误:

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
4

1 回答 1

0

它直接在 app/java 文件夹下

那是在您的开发机器上。这不在Android设备上。

如果您想将 JSON 与您的 Android 应用程序一起提供,请将其放入assets/. 然后,用于AssetManager获取InputStream资产以传递给所需的 JSON 解析器。

于 2016-03-08T20:52:53.833 回答