2017-03-22 1 views
1

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?

+0

Que dit la trace de la pile? – ApolloSoftware

+0

Thread thread = nouveau Thread(); – ApolloSoftware

+0

Le bloc try n'est pas exécuté. @marcinj dit que j'ai oublié "démarrer", c'est vrai: o –

Répondre

4

vous avez oublié d'appeler .start():

new Thread() { // last BreakPoint stop here 
    // 
}.start; 
^^^^^^^ 
+0

LOL, j'ai oublié de commencer ... –

1

je aimerais charger le fil dans une variable t et vous pouvez l'exécuter est des méthodes membres sur comme start(), join(), le sommeil , céder, interrompre, etc.

Thread t = 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(); 
      } 
     } 
    }; 
t.start();