Ajout de points d'arrêt Je peux voir que le thread ci-dessous n'est pas exécuté. Je fais la recherche zippée pour ZIPCode en utilisant HTTP, renvoyant un JSON, via le service web de viacep.com.Pourquoi le thread ne fonctionne pas?
if (code.length() == 8) {
final ProgressDialog dialog = ProgressDialog.show(Inicial.this, "",
"Loading ZipCode", true);
dialog.show();
new Thread() { // last BreakPoint stop here
public void run() {
try {
//This code isn't running
String url = "https://viacep.com.br/ws/" + code + "/json";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
final HttpResponse resposta = httpClient.execute(httpPost);
runOnUiThread(new Runnable() {
public void run() {
try {
JSONObject obj = new JSONObject(EntityUtils.toString(resposta.getEntity()));
EditText endereco = (EditText) findViewById(R.id.cadEndereco);
EditText compl = (EditText) findViewById(R.id.cadComplemento);
EditText bairro = (EditText) findViewById(R.id.cadBairro);
EditText cidade = (EditText) findViewById(R.id.cadCidade);
EditText uf = (EditText) findViewById(R.id.cadUF);
endereco.setTag(obj.getString("logradouro"));
compl.setText(obj.getString("complemento"));
bairro.setText(obj.getString("bairro"));
cidade.setText(obj.getString("localidade"));
uf.setText(obj.getString("uf"));
} catch (IOException | JSONException e) {
e.printStackTrace();
}
dialog.dismiss();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
Ainsi, la boîte de dialogue de chargement montre pour le temps non défini parce dialog.dismiss() est sur le runOnUIThread. Quelqu'un sait pourquoi cela ne fonctionne pas?
Que dit la trace de la pile? – ApolloSoftware
Thread thread = nouveau Thread(); – ApolloSoftware
Le bloc try n'est pas exécuté. @marcinj dit que j'ai oublié "démarrer", c'est vrai: o –