Ho un'attività (RecipiesActivity) che si apre facendo clic sul pulsante "Avanti" dell'Attivit principale della mia applicazione.onPostExecute non viene chiamato
In RecipiesActivity onCreate, desidero chiamare un servizio Web in un AsyncTask. Per ora, dal momento che non ho ancora implementato il servizio web, sto solo chiamando un servizio web fornito da bitly (ma questo è irrilevante per il mio problema).
Il problema è che sebbene il servizio Web venga chiamato e non venga generata alcuna eccezione, viene restituito un risultato da doInBackground, ma myPostExecute non viene chiamato. Ho fatto qualche ricerca e ho trovato le possibili ragioni di seguito elencati - ma nessuno sembra essere il mio caso:
- parametri OnPostExecute non corrispondono ai parametri AsyncTask - nel mio caso corrispondono
- Un bug in Android che non consente di eseguire AsyncTask nel thread dell'interfaccia utente. E 'supposadely supera utilizzando Class.forName ("android.os.AsyncTask") - Ho provato questo e non faceva alcuna differenza
- AsyncTask non sia partito dal thread UI - nel mio caso credo che sia
- doInBackground non restituisce - nel mio caso lo fa, ho fatto un passo attraverso di essa con il debugger
- @Override non viene utilizzata prima OnPostExecute - nel mio caso io sto usando @Override
il codice è qui sotto:
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);
}
}
}
}
Il vostro aiuto è appreceiated,
grazie
Grazie mille. Questo (ovvio) errore ha causato il problema. – Lefteris
contrassegnare la soluzione come accettata ... – Hades
Stavo per farlo subito dopo che è stato pubblicato, ma mi ha richiesto di aspettare circa 7-10 minuti prima di farlo. In ogni caso, la risposta è contrassegnata ora :) – Lefteris