Handling Android Async Task Failure
<p>My app POSTs JSON to an API which upon success returns a response. It usually works (90% of the time), but occasionally the user presses the sync button and nothing seems to happen. In the background the Async Task may have run but the UI never gets updated and the app gets no response to handle.
The users issue can be resolved by restarting the tablet, opening the app and doing the sync again and it will work first time. 
<strong>What is it the restarting the tablet might be doing that fixes this issue?</strong> </p>

<p>This is my Sync method</p>

<pre><code>private void Sync()
{
    if (connectedToNetwork())
    {
        syncStatus.setImageResource(0);
        syncStatus.setTag(0);
        status.setText(""); // Clear out text
        mProgressDialog.setMessage("Please wait for the sync to complete...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();

        new SyncJob().execute();
    }
    else
    {
        // Do nothing
        HelperMethods.ShowToast(getString(R.string.wifi_not_connected), getApplicationContext());
    }
}
</code></pre>

<p>This calls the async task</p>

<pre><code>   class SyncJob extends AsyncTask&lt;String, String,SyncResponse&gt;
{
    private Response response=null;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

    }

    protected SyncResponse doInBackground(String... args) {

        SyncResponse syncResponse = new SyncResponse();

        // Get JWT
        String hash = HelperMethods.GenerateToken(settings.getAudienceName(), settings.getAuthPrivateApiKey(), settings.getRequestedResource(), HelperMethods.getTicks());
        response = JSONParser.makeHttpTokenRequest(url_get_jwt, "GET", null, hash);

        String jwt = response.getToken();

        if (response.getResponseCode()==200 &amp;&amp; jwt != null) {
            syncResponse.setGetJWTSucceeded(true);
            publishProgress("\nAuthenticated");
        }
        else
        {
            if (response.isUnknownHostException()) {
                publishProgress("\nUnknown Host Exception - Check the internet connection");
            }
            else {
                publishProgress("\nFailed to Authenticate");
            }
            syncResponse.setGetJWTSucceeded(false);
            return syncResponse;
        }

        // Post candidates
        List&lt;Candidate&gt; listOfCandidates = HelperMethods.GetCandidates(SyncActivity.this);
        if (listOfCandidates != null)
        {
            int count = listOfCandidates.size();
            publishProgress("\n" + count +  " candidates to be uploaded");
            String jarray = HelperMethods.GetCandidatesAsJSONString(listOfCandidates, versionName, settings.getCountryCode());

            response = JSONParser.makeHttpPostRequest(url_create_candidate, "POST", jarray, jwt);

            if (response.getResponseCode()==200)
            {
                syncResponse.setPostCandidateSucceeded(true);
                publishProgress(HelperMethods.ProcessCandidatePOSTResponse(response.getjObj(), SyncActivity.this));
            }
            else
            {
                if (response.isUnknownHostException()) {
                    publishProgress("\nUnknown Host Exception - Check the internet connection");
                }
                publishProgress("\nUnable to upload Candidates");
                syncResponse.setPostCandidateSucceeded(false);
            }
        }
        else
        {
            publishProgress("\nNothing to upload");
        }

        // Get Subjects
        publishProgress("\nUpdating subjects...");
        response = JSONParser.makeHttpGetRequest(url_all_subjects, "GET", null, jwt);
        try {
            JSONArray subjects = response.getjObj().getJSONArray(TAG_SUBJECTS);
            if (response.getResponseCode()==200)
            {
                syncResponse.setGetSubjectsSucceeded(true);
                String numberSubjectsAdded = HelperMethods.AddSubjects(SyncActivity.this, subjects);
                publishProgress("\n" + numberSubjectsAdded);
            }
            else
            {
                if (response.isUnknownHostException()) {
                    publishProgress("\nUnknown Host Exception - Check the internet connection");
                }
                syncResponse.setGetSubjectsSucceeded(false);
                publishProgress("\nUnable to download subjects");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        // Update events
        publishProgress("\nUpdating events...");
        response = JSONParser.makeHttpGetEventsRequest(url_get_events, "GET", null, jwt);
        try {
            JSONArray events = response.getjObj().getJSONArray(TAG_EVENTS);
            if (response.getResponseCode()==200)
            {
                //syncResponse.setGetSubjectsSucceeded(true);
                String numberEventsAdded = HelperMethods.AddEvents(SyncActivity.this, events);
                publishProgress("\n" + numberEventsAdded);
            }
            else
            {
                if (response.isUnknownHostException()) {
                    publishProgress("\nUnknown Host Exception - Check the internet connection");
                }
                //syncResponse.setGetSubjectsSucceeded(false);
                publishProgress("\nUnable to download events");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return syncResponse;
    }

    protected void onPostExecute(SyncResponse response) {

        if (response.PostCandidateSucceeded())
        {
            syncStatus = (ImageView)findViewById(R.id.syncStatusImg);
            syncStatus.setTag(R.drawable.success);
            syncStatus.setImageResource(R.drawable.success);
        }

        try {
            mProgressDialog.dismiss();
        } catch (Exception e) {

        }
    }

    @Override
    protected  void onProgressUpdate(String...progress) {

        status = (TextView) findViewById(R.id.uploadStatusTextView);
        status.setText(status.getText() + progress[0]);
    }

}
</code></pre>

<p>Any help would be much appreciated</p>