AsyncTask的机器人 - 为ExecutionException错误 [英] AsyncTask android - ExecutionException error

查看:279
本文介绍了AsyncTask的机器人 - 为ExecutionException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这有code我的主类中:

I've got this code inside my main class:

  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;
}

和在此行结果
字符串输出=新vu​​rlDownloader()的execute()获得(); 结果
Android的工作室给我奇怪的引用(红色下划线):
未处理的异常: java.lang.InterruptedException,java.util.concurrent.Execution.Exception

我不明白这一点,becouse我有如下呈三角情况:<一href=\"http://stackoverflow.com/questions/10972114/android-getting-string-back-from-asynctask\">Android让字符串返回从AsyncTask的和这个人很有效。

I don't quite understand this, becouse I've got simillar situation as here: Android getting string back from AsyncTask and for this person it works.

您好!

推荐答案

的get()抛出java.lang.InterruptedException。你需要有一个尝试捕捉,抓住那些

get() throws java.lang.InterruptedException. You need to have a try catch and catch those

http://developer.android.com/reference/安卓/ 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的没有更多的异步。

But you should not use get as it blocks the ui thread waiting for the result. This makes AsyncTask no more Asynchronous.

解决方案

 new vurlDownloader().execute()

和您可以更新 onPostExecute UI如果AsyncTask的是一个内部类,或者使用接口作为回调的活动。

And You can update ui in onPostExecute if Asynctask is an inner class or use interface as a callback to the Activity.

检查blackbel的回答@

Check blackbel's answer @

如何从AsyncTask的返回一个布尔值?

一个建议:遵循Java命名约定重命名 vurlDownloader() VurlDownloader()。类名开头大写

A suggestion : Follow java naming conventions rename vurlDownloader() with VurlDownloader(). Class name begins with caps

编辑:

要调用

 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);
    }

}

这篇关于AsyncTask的机器人 - 为ExecutionException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆