Android : Error in creating json file locally in internal storage

on Saturday, August 2, 2014


I want to store json file internally from server. For that I am using below method. but I got error. Please help on it.



try {
// catches IOException below
final String TESTSTRING =jsonBranchArray.toString();

/* We have to use the openFileOutput()-method
* the ActivityContext provides, to
* protect your file from others and
* This is done for security-reasons.
* We chose MODE_WORLD_READABLE, because
* we have nothing to hide in our file */
FileOutputStream fOut = openFileOutput("samplefile.json",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);

// Write the string to the file
osw.write(TESTSTRING);

/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();

//Reading the file back...

/* We have to use the openFileInput()-method
* the ActivityContext provides.
* Again for security reasons with
* openFileInput(...) */

FileInputStream fIn = openFileInput("samplefile.json");
InputStreamReader isr = new InputStreamReader(fIn);

/* Prepare a char-Array that will
* hold the chars we read back in. */
char[] inputBuffer = new char[TESTSTRING.length()];

// Fill the Buffer with data from the file
isr.read(inputBuffer);

// Transform the chars to a String
String readString = new String(inputBuffer);

// Check if we read back the same chars that we had written out
boolean isTheSame = TESTSTRING.equals(readString);

Log.i("File Reading stuff", "success = " + isTheSame);


} catch (IOException ioe) {
ioe.printStackTrace();
}


And error is.. 08-02 12:26:13.449: W/dalvikvm(23547): threadid=1: thread exiting with uncaught exception (group=0x40015560) 08-02 12:26:13.459: E/AndroidRuntime(23547): FATAL EXCEPTION: main 08-02 12:26:13.459: E/AndroidRuntime(23547): java.lang.NullPointerException 08-02 12:26:13.459: E/AndroidRuntime(23547): at com.example.MainActivity.WriteToFile(MainActivity.java:224) 08-02 12:26:13.459: E/AndroidRuntime(23547): at com.example.MainActivity.onClick(MainActivity.java:90) 08-02 12:26:13.459: E/AndroidRuntime(23547): at android.view.View.performClick(View.java:2485) 08-02 12:26:13.459: E/AndroidRuntime(23547): at android.view.View$PerformClick.run(View.java:9080) 08-02 12:26:13.459: E/AndroidRuntime(23547): at android.os.Handler.handleCallback(Handler.java:587) 08-02 12:26:13.459: E/AndroidRuntime(23547): at android.os.Handler.dispatchMessage(Handler.java:92) 08-02 12:26:13.459: E/AndroidRuntime(23547): at android.os.Looper.loop(Looper.java:123) 08-02 12:26:13.459: E/AndroidRuntime(23547): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-02 12:26:13.459: E/AndroidRuntime(23547): at java.lang.reflect.Method.invokeNative(Native Method) 08-02 12:26:13.459: E/AndroidRuntime(23547): at java.lang.reflect.Method.invoke(Method.java:507) 08-02 12:26:13.459: E/AndroidRuntime(23547): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-02 12:26:13.459: E/AndroidRuntime(23547): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-02 12:26:13.459: E/AndroidRuntime(23547): at dalvik.system.NativeStart.main(Native Method)


and sample json is..



{
"Title" : "........Name....",
"Logo" : "http://test/1.jpg",
"ID" : "1015",
"Contact":{
"Title" : " something ",
"Image" : "http://test/1.jpg",
"ID" : "1016",
"Full Address" : " something something something something something ",
"Contact No" : ".....Post Date with Time....",
"Mail ID" : ".....Start Date with Time....",
"SiteLink" : "...wwww.abc.com......",
"LandLine No" : ".....End Date with Time...."
},
"Activities" : {"News":[
{
"Image" : "http://test/1.jpg",
"ID" : "1017",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."
},{
"Image" : "http://test/2.jpg",
"ID" : "1018",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."

},{
"Image" : "http://test/3.jpg",
"ID" : "1019",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."


},{
"Image" : "http://test/4.jpg",
"ID" : "1020",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."

},{
"Image" : "http://test/5.jpg",
"ID" : "1021",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."

},{
"Image" : "http://test/6.jpg",
"ID" : "1022",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."

},{
"Image" : "http://test/7.jpg",
"ID" : "1023",
"Title" : " something ",
"Description" : " something something something something something ",
"Date" : ".....Post Date...."

}
],
"Events":[
{
"Image" : "http://test/7.jpg",
"ID" : "1024",
"Title" : " something ",
"Description" : " something something something something something ",
"Location" : "......Event Loc......",
"Start Date" : ".....Start Date with Time....",
"End Date" : ".....End Date with Time...."
},{
"Image" : "http://test/3.jpg",
"ID" : "1025",
"Title" : " something ",
"Description" : " something something something something something ",
"Location" : "......Event Loc......",
"Start Date" : ".....Start Date with Time....",
"End Date" : ".....End Date with Time...."

},{
"Image" : "http://test/4.jpg",
"ID" : "1026",
"Title" : " something ",
"Description" : " something something something something something ",
"Location" : "......Event Loc......",
"Start Date" : ".....Start Date with Time....",
"End Date" : ".....End Date with Time...."

},{
"Image" : "http://test/1.jpg",
"ID" : "1027",
"Title" : " something ",
"Description" : " something something something something something ",
"Location" : "......Event Loc......",
"Start Date" : ".....Start Date with Time....",
"End Date" : ".....End Date with Time...."

}
]...as so on

0 comments:

Post a Comment