OnPostExecute not called

I have an Activity (RecipiesActivity) that opens by clicking the "Next" button of the main asset of my application.

In RecipiesActivity onCreate, I want to call a web service in AsyncTask. At the moment, since I have not yet implemented a web service, I am just calling the web service provided in bits (but this is not relevant to my problem).

The problem is that although the web service receives the call and no exception is thrown, the result is a return from doInBackground, but my onPostExecute is not called. I did some research and I found the possible causes listed below, but none of them are my case:

  • OnPostExecute parameters do not match AsyncTask parameters - in my case they match
  • An error in android that does not allow the execution of AsyncTask in the user interface thread. It is supposedly overridden using Class.forName ("android.os.AsyncTask") - I tried this and it didn't matter.
  • AsyncTask does not start from the user interface thread - in my case, I believe that this
  • doInBackground does not return - in my case this happens, I went through it using the debugger
  • @Override is not used until onPostExecute - in my case I use @Override

Code below:

public class RecipiesActivity extends Activity {

    private TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_recipies); 
        tv = (TextView) findViewById(R.id.Response);

        //Start the WS call in an AsyncTask 
        new CallWS().doInBackground("https://api-ssl.bitly.com/v3/shorten?login=maskedLogin&apiKey=maskedKey&longUrl=http%3A%2F%2Fgoogle.com%2F");          
    }

    private class CallWS extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... params) {

            String result = null;
            HttpClient httpClient = new DefaultHttpClient(); 
            HttpContext localContext = new BasicHttpContext(); 
            HttpGet get_request = new HttpGet(params[0]); 
            try
            {
                HttpResponse httpResponse = httpClient.execute(get_request, localContext);
                //int responseCode = httpResponse.getStatusLine().getStatusCode();
                HttpEntity entity = httpResponse.getEntity();
                if (entity != null)
                {
                    InputStream istream = entity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(istream));

                    try
                    {
                        result = br.readLine();
                    }
                    catch (Exception e)
                    {
                        //TODO Handle the IOException that the readline may throw 
                    }
                    finally
                    {
                        istream.close();
                    }
                }

            }catch (Exception e)
            {
                //TODO Handle the IOException and the ClientProtocolException that the 
                // httpClient.execute can throw
            }
            finally
            {
                httpClient.getConnectionManager().shutdown();
            }
            return result;
        } 

        @Override
        protected void onPostExecute(String result)
        {
            if (result == null)
            {
                tv.setText("The result is NULL");
            }
            else
            {
                tv.setText(result);
            }
        }       
    }
}

Your help is appreciated

thank

+5
source share
3 answers

doInBackground() execute() AsyncTask. doInBackground() , : doInBackground() ( ) . onPostExecute() . execute() , doInBackground() , doInBackground() onPostExecute() .

+5

new CallWS().doInBackground("https://api-ssl.bitly.com/v3/shorten?login=maskedLogin&apiKey=maskedKey&longUrl=http%3A%2F%2Fgoogle.com%2F");

to

new CallWS().execute("https://api-ssl.bitly.com/v3/shorten?login=maskedLogin&apiKey=maskedKey&longUrl=http%3A%2F%2Fgoogle.com%2F");
+3

Make sure the onPostExecute function has @Override

+1
source

All Articles