Android AsyncTask (returning an Integer from doInBackground)

I'm getting the following errors in the code below: The return type is incompatible with AsyncTask.onPostExecute(Integer). I'm trying to return the result from the http request done in the doInBackground task. I get the error: Type mismatch: cannot convert from AsyncTask to int in the return statement for isAvailable. I feel like there's something simple I'm not doing but I can't quite figure it out.

    public int isAvailable(int position) {
        GetIsAvailable isAvail = new GetIsAvailable();
        Integer nisAvail = isAvail.execute(position); // error is still here
        return nisAvail;


    private class GetIsAvailable extends AsyncTask<Integer,Void,Integer > {

        protected Integer doInBackground(Integer...position) {
            Bundle resBundle = new Bundle();
            String url = ""
                + position[0]+"&uname="+AppStatus.mUserName;
            URL iuri;
            try {
                iuri = new URL(url);
                URLConnection connection = iuri.openConnection();
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        (InputStream) connection.getContent()));
                resBundle.putInt("isAvail", Integer.parseInt(br.readLine().trim()));
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            return new Integer(0);


        protected Integer onPostExecute(Integer isAvail) { // Main Error here

            return isAvail; 


Oh, I think I see the problem. I don't think you can handle this the way you're doing it currently. You should be handling the effects of the value of isAvail within the onPostExecute() method. isAvailable() is running on the main thread, while isAvail is running on a separate thread. You're trying to return the result of the AsyncTask before it has finished completion.

I'm 99% sure that's the problem.

I believe what you're looking for is

Integer nisAvail = isAvail.execute(position).get();

but then the task is no longer asynchronous as the UI thread has to wait until the AsyncTask finishes.

If you want to keep it asynchronous then you have to handle the result in onPostExecute.

Need Your Help

margin within a subset plot

r graphics plot margin

When we select the entire data range, we get a plot with a slight margin inside the plot region so that the plot doesn't touch the bounding box. However when we subset a plot, the margin is gone. Is

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.