【发布时间】:2014-09-25 09:22:03
【问题描述】:
我想从服务器内部存储 json 文件。为此,我正在使用以下方法。但我得到了错误。请帮忙。
try {
final String TESTSTRING =jsonBranchArray.toString();
FileOutputStream fOut = openFileOutput("samplefile.json",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(TESTSTRING);
osw.flush();
osw.close();
FileInputStream fIn = openFileInput("samplefile.json");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[TESTSTRING.length()];
isr.read(inputBuffer);
String readString = new String(inputBuffer);
boolean isTheSame = TESTSTRING.equals(readString);
Log.i("File Reading stuff", "success = " + isTheSame);
} catch (IOException ioe) {
ioe.printStackTrace();
}
错误是.. 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):致命异常:主要 08-02 12:26:13.459: E/AndroidRuntime(23547): java.lang.NullPointerException 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 com.example.MainActivity.WriteToFile(MainActivity.java:224) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 com.example.MainActivity.onClick(MainActivity.java:90) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 android.view.View.performClick(View.java:2485) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 android.view.View$PerformClick.run(View.java:9080) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 android.os.Handler.handleCallback(Handler.java:587) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 android.os.Handler.dispatchMessage(Handler.java:92) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 android.os.Looper.loop(Looper.java:123) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 java.lang.reflect.Method.invoke(Method.java:507) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-02 12:26:13.459: E/AndroidRuntime(23547): 在 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)
示例 json 是..
{
"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
【问题讨论】:
-
1) 使用
source标签发布堆栈跟踪,以便于阅读。 2)在代码中突出显示这一行,异常源自com.example.MainActivity.WriteToFile(MainActivity.java:224) -
那么 MainActivity.java 的第 224 行是什么? (在 com.example.MainActivity.WriteToFile(MainActivity.java:224))
-
我不得不说,这是我见过的过度评论最好的例子。这么多文字说这么少。
-
最终字符串 TESTSTRING =jsonBranchArray.toString(); --224行
标签: java android json offline-caching