-1

EDITED

So I am trying to write a code that writes to a .Json but my app keeps crashing on me.

I follow serval tutorials on what to do but non of them help.

My code is as Follows:

AndroidManifest:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name = "android.permission.WRITE_INTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

MainActivity:

private static final String FILE_NAME = "MyDataSheet.json";

...

public String chkFile(Context context) { String json = null;

try {
    InputStream is = context.getAssets().open(FILE_NAME);
    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;
}

...

protected  void sdBTN(View v, Context context)
{
    String txt1 = input1.getText().toString();
    String txt2 = input2.toString();
    String txt3 = unput3.toString();

JSONObject object = null;
try {
    object = new JSONObject(chkFile(this));
} catch (JSONException e) {
    e.printStackTrace();
}


try {
    object.put("name", text1);
    object.put("googleSheet", myGoogleSheet);
    object.put("googleScript", myGoogleScript);

} catch (JSONException e) {
    e.printStackTrace();
}
System.out.println(object);

}

I created the file in a assets folder but the app is Still exhibiting the same result of closing on me, as with my previous .txt attempt.

I follow tutorials, but non help.

1 Answers1

1

You should check the console log output to see the actual error message to include in your question. Make use of Android Logging classes. Add logging like in this format:

import android.util.Log;
...
private static final String TAG = "MyAppName";
Log.i(TAG, "Some info message such as mkdir: "+file);

if (BuildConfig.DEBUG) {
   Log.d(TAG, "Some debug message here");
}

Get rid of e.printStackTrace() and use:

Log.e(TAG, "Failed to save:"+file, e);

While your application is running after launch from Android Studio and when connected to a device on USB, go to the Logcat tab of Android Studio. The Logcat tab allows filter on name - just type in "MyAppName" then run your test again.

You have 'file.mkdir();' in chkFile() referencing FILE_NAME which is also passed to openFileOutput(FILE_NAME...) so maybe you have created the file path as a directory before opening that path to write a file.

DuncG
  • 12,137
  • 2
  • 21
  • 33