wzieba
wzieba

Reputation: 414

AsyncTask android - ExecutionException error

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

and in this line
String output = new vurlDownloader().execute().get();
Android Studio gives me strange reference (red underline): Unhandled exceptions: java.lang.InterruptedException, java.util.concurrent.Execution.Exception

I don't quite understand this, becouse I've got simillar situation as here: How to get a string back from AsyncTask? and for this person it works.

Greetings!

Upvotes: 0

Views: 3981

Answers (3)

M D
M D

Reputation: 47817

Try to implement your AsyncTask this way and you can get Result string into onPostExecute() as onBackground(...) method return

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

 }
}

And called as

 new vurlDownloader().execute()

Go to this for more information:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133570

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

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.

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

Solution

 new vurlDownloader().execute()

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

Check blackbel's answer @

How do I return a boolean from AsyncTask?

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

Edit:

To invoke

 vurlDownloader().execute();

Then

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

}

Upvotes: 4

Solution
Solution

Reputation: 602

Add Post execute method in your async task

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

 }

Add one other method that is return final result of async task.

  public String getFinalResult() {
        return finalResult;
  }

When you need final result you can use getFinalResult method to get.

Upvotes: 0

Related Questions