AsyncTask android - ExecutionException错误

时间:2014-02-25 10:10:53

标签: java android android-asynctask

我的主要类中有这个代码:

  public class vurlDownloader extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {

        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpGet httpGet = new HttpGet("http://url/video.html");
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpGet, localContext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = "";

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent()
                    )
            );
        } catch (IOException e) {
            e.printStackTrace();
        }


        String line = null;
        try {
            while ((line = reader.readLine()) != null){
                result += line + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;

    }

}

public String loadVurl(){
    String output = new vurlDownloader().execute().get();
    return output;
}

并在此行中 String output = new vurlDownloader().execute().get();
Android Studio给了我奇怪的参考(红色下划线): 未处理的例外:java.lang.InterruptedException, java.util.concurrent.Execution.Exception

我不太明白这一点,因为我在这里遇到了类似情况:How to get a string back from AsyncTask?并且对于这个人来说它是有效的。

问候!

3 个答案:

答案 0 :(得分:3)

get()抛出java.lang.InterruptedException。你需要试一试并抓住那些

http://developer.android.com/reference/android/os/AsyncTask.html#get()

public final Result get ()

Added in API level 3
Waits if necessary for the computation to complete, and then retrieves its result.

Returns
The computed result.
Throws
CancellationException   If the computation was cancelled.
ExecutionException  If the computation threw an exception
InterruptedException    If the current thread was interrupted while waiting.

但是你不应该使用get,因为它会阻止等待结果的ui线程。这使得AsyncTask不再是异步。

解决方案

 new vurlDownloader().execute()

如果Asynctask是一个内部类或者使用接口作为Activity的回调,你可以在onPostExecute中更新ui。

检查blackbel的回答@

How do I return a boolean from AsyncTask?

建议:遵循java命名约定,使用vurlDownloader()重命名VurlDownloader()。班级名称以大写字母开头

编辑:

调用

 vurlDownloader().execute();

然后

class vurlDownloader() extends AsyncTask<Void,Void,String>
{

    @Override
    protected String doInBackground(Void... params) {

         String _response= null;

                    try {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpContext localContext = new BasicHttpContext();
                        HttpGet httpGet = new HttpGet("http://informat.net.pl/krachapp/video.html");
                        HttpResponse response = null;
                        response = httpClient.execute(httpGet, localContext);
                         _response = EntityUtils.toString(response.getEntity());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }    



        return _response;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Log.i("Result is",result);
    }

}

答案 1 :(得分:1)

尝试以这种方式实施 AsyncTask ,您可以将Result字符串转换为onPostExecute()作为onBackground(...)方法返回

private class vurlDownloader() extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    try {
         String _result= null;

                try {
                    HttpClient httpClient = new DefaultHttpClient();
                    HttpContext localContext = new BasicHttpContext();
                    HttpGet httpGet = new HttpGet("http://informat.net.pl/krachapp/video.html");
                    HttpResponse response = null;
                    response = httpClient.execute(httpGet, localContext);
                     _result = EntityUtils.toString(response.getEntity());
                } catch (IOException e) {
                    e.printStackTrace();
                }    

    } catch (InterruptedException e) {
        Log.e("LongOperation", "Interrupted", e);
        return "Interrupted";
    }
    return _result;
}      

@Override
protected void onPostExecute(String result) {               
  system.out.print(result);

 }
}

并称为

 new vurlDownloader().execute()

转到此处获取更多信息:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

答案 2 :(得分:0)

在异步任务中添加Post执行方法

 @Override
 protected void onPostExecute(String result) {
       Log.d("RESULT", "Result : " + result);
       finalResult = result; // here finalResult is string variable declared at class level

 }

添加另一个返回异步任务最终结果的方法。

  public String getFinalResult() {
        return finalResult;
  }

当您需要最终结果时,可以使用getFinalResult方法获取。