带有readObject()的AsyncTask

时间:2013-04-11 11:23:43

标签: android android-asynctask local-storage

util.concurrent.FutureTask.finishCompletion  当我尝试在内部存储中打开文件时,我不知道如何处理,如果你有一些建议请帮助我

我的代码:

      protected String doInBackground(Hashtable<String,String>... params) {
       Hashtable ht=params[0];

       String json=HelperHttp.getJSONResponseFromURL(url, ht);
       System.out.println(json);

        try {
            jObj = new JSONObject(json);
              ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
                out.writeObject(jObj.toString() );
                out.close();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



       if(json!=null) {try {
          ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
           JSONObject jsonObject;
        try {
            jsonObject = (JSONObject) in.readObject();
            JSONArray articles = jsonObject.getJSONArray("posts");

             for(int i = 0; i < articles.length(); i++){
                    JSONObject c = articles.getJSONObject(i);

                    // Storing each json item in variable
                    AO=c.getJSONObject("post");


                    String fr_titre = AO.getString("fr_titre");
                   Log.d("hhhhhhhhhh", fr_titre);

                   lista.add(fr_titre);

        }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           in.close(); 


         } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ObjectStreamException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    } else{
        return "Invalid Company Id";
       }

       return "SUCCESS";
      }

stacktrace:

04-11 11:52:04.694: E/AndroidRuntime(1026): FATAL EXCEPTION: AsyncTask #1
04-11 11:52:04.694: E/AndroidRuntime(1026): java.lang.RuntimeException: An error occured while executing doInBackground()
04-11 11:52:04.694: E/AndroidRuntime(1026):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.lang.Thread.run(Thread.java:856)
04-11 11:52:04.694: E/AndroidRuntime(1026): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.json.JSONObject
04-11 11:52:04.694: E/AndroidRuntime(1026):     at com.cgi.activity.ActivityListNews$GetDeptAyncTask.doInBackground(ActivityListNews.java:81)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at com.cgi.activity.ActivityListNews$GetDeptAyncTask.doInBackground(ActivityListNews.java:1)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-11 11:52:04.694: E/AndroidRuntime(1026):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-11 11:52:04.694: E/AndroidRuntime(1026):     ... 3 more

1 个答案:

答案 0 :(得分:2)

你写一个字符串:

out.writeObject(jObj.toString() );

但是在阅读它时将它转换为JSONObject:

jsonObject = (JSONObject) in.readObject();

您可以阅读它,将其转换为String然后解析它:

jsonObject = new JSONObject((String) in.readObject());