【问题标题】:Error in creating json file locally in internal storage在内部存储中本地创建 json 文件时出错
【发布时间】: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


【解决方案1】:

从错误中我可以看到您没有使用异步任务并生成网络请求,添加异步任务并在单独的线程中执行网络请求!

       class GetContacts extends AsyncTask<Void, String, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();


        }

        @Override
        protected Void doInBackground(Void... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient();
            try {
                HttpGet httpget = new HttpGet(
                        "url");
                HttpResponse response = httpclient.execute(httpget);
                String jsonResp = EntityUtils.toString(response.getEntity());
                Log.d("HTTP","Rsponse : "+ jsonResp);

                JSONObject jsonObject = new JSONObject(jsonResp);

                /// ADD YOUR CODE HERE------------------------------------------------------------->


            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

        }
    }

【讨论】:

  • @user3879110 .... 您是否更新了onPostExecute 中与 UI 相关的值?
  • no.. 我试图从 url 获取数据并将其保存在内存中。
  • @user3879110 .... 在doInBackground 中尝试fetch data from url,在onPostExecute 中尝试save it in internal memory
  • ya.. 我也做过.. 但最终 String TESTSTRING =jsonBranchArray.toString(); -- 这一行..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-08
  • 2020-03-24
  • 2012-10-22
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多