【问题标题】:How to properly handle Async Task ending?如何正确处理异步任务结束?
【发布时间】:2012-04-17 14:28:41
【问题描述】:

我正在使用 AsyncTask 从服务器获取我的应用所需的所有数据。这工作正常。但是,当第二次重新启动应用程序时,应用程序崩溃了。

我收到以下错误日志:

04-17 14:22:57.171: E/AndroidRuntime(956): FATAL EXCEPTION: AsyncTask #2
04-17 14:22:57.171: E/AndroidRuntime(956): java.lang.RuntimeException: An error occured while executing doInBackground()
04-17 14:22:57.171: E/AndroidRuntime(956):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.lang.Thread.run(Thread.java:856)
04-17 14:22:57.171: E/AndroidRuntime(956): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=14; index=14
04-17 14:22:57.171: E/AndroidRuntime(956):  at flexform.ro.android.app.FlexFormActivity$getDataClass.doInBackground(FlexFormActivity.java:88) 
04-17 14:22:57.171: E/AndroidRuntime(956):  at flexform.ro.android.app.FlexFormActivity$getDataClass.doInBackground(FlexFormActivity.java:1)
04-17 14:22:57.171: E/AndroidRuntime(956):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-17 14:22:57.171: E/AndroidRuntime(956):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

下面是我的 asynctask 类:

class getDataClass extends AsyncTask<String, Void, String>{
    protected String doInBackground(String...urls){
        String response = "";
        for(String url : urls){
            response = "";
            descriptionArray_Counter++;

            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try{
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(content));

                String s = "";

                while((s = buffer.readLine()) != null){
                    response += s;
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            Resources.descriptionArray[descriptionArray_Counter] = response;
        }
        return response;
    }

    protected void onPostExecute(String result){
        Toast.makeText(getApplicationContext(), "All entres have been loaded!", Toast.LENGTH_SHORT).show();

        JSONArray offersJSON;
        JSONArray newsJSON;

        try{
            newsJSON = new JSONArray(Resources.descriptionArray[9]);

            if(newsJSON.length() > 20)
                Resources.newsLength = 20;
            else
                Resources.newsLength = newsJSON.length();

            for(int i = 0; i < Resources.newsLength; i++){
                JSONObject tempObject = newsJSON.getJSONObject(i);

                Resources.news_id[i] = tempObject.getString("id");
                Resources.news_title[i] = tempObject.getString("title");
                Resources.news_descriptionShort[i] = tempObject.getString("description_short");
                Resources.news_descriptionLong[i] = tempObject.getString("description_long");
                Resources.news_date[i] = tempObject.getString("date");
                Resources.news_urgent[i] = tempObject.getString("urgent");
            }
        }catch(JSONException e){
            e.printStackTrace();
        }
        try {
            offersJSON = new JSONArray(Resources.descriptionArray[10]);


        if(offersJSON.length() > 20)
            Resources.offersLength = 20;
        else
            Resources.offersLength = offersJSON.length();

        for(int i = 0; i < Resources.offersLength; i++){
            JSONObject tempObject = offersJSON.getJSONObject(i);

            Resources.offers_id[i] = tempObject.getString("id");
            Resources.offers_offertant[i] = tempObject.getString("Ofertant");
            Resources.offers_description[i] = tempObject.getString("Descriere");
            Resources.offers_contact[i] = tempObject.getString("Contact");
            Resources.offers_date[i] = tempObject.getString("Data");
            Resources.offers_available[i] = tempObject.getString("Valabil");

        } 
        Intent intent = new Intent(FlexFormActivity.this, MainMenu.class);
        startActivity(intent);

        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

我希望有更多经验的人能够向我解释解决这个崩溃的正确、正确的方法......

干杯并感谢任何花时间回答并帮助我的人......

【问题讨论】:

    标签: android json http android-asynctask forceclose


    【解决方案1】:

    我怀疑您永远不会将您的 descriptionArray_Counter 重置为 0。因此,当您执行任务时,请执行以下行

    Resources.descriptionArray[descriptionArray_Counter] = response;
    

    可能正在评估

    Resources.descriptionArray[14] = response;
    

    并且您的描述数组的长度仅为 14。您可以在堆栈跟踪中看到它准确地告诉了您这一点。

    【讨论】:

    • 对不起...现在感觉很笨。已经编码了几个小时,只是错过了它。非常感谢您查看代码并回答。
    • 您的代码的另一个可疑部分是您在 for 循环开始时递增 descriptionArray_Counter。您确定在数组中设置响应后不打算这样做吗?
    【解决方案2】:

    您收到 arrayIndexOutOfBounds 错误。您对数组的调用之一,特别是在第 88 行,超出了范围。 asynchTask 不是问题。

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多