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.