为什么线程不 运行?
Why Thread don't run?
加断点看到下面的线程没有执行。
我使用 HTTP 对 ZIPCode 进行压缩搜索,通过 viacep.com 网络服务返回 JSON。
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();
}
}
};
}
因此,加载对话框的显示时间未定义,因为 dialog.dismiss() 在 runOnUIThread 上。有人知道为什么这不起作用吗?
您忘记调用 .start():
new Thread() { // last BreakPoint stop here
//
}.start;
^^^^^^^
我会将 Thread 加载到变量 t 中,然后您可以 运行 它的成员方法,如 start()、join()、sleep、yield、interrupt 等。
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();
加断点看到下面的线程没有执行。 我使用 HTTP 对 ZIPCode 进行压缩搜索,通过 viacep.com 网络服务返回 JSON。
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();
}
}
};
}
因此,加载对话框的显示时间未定义,因为 dialog.dismiss() 在 runOnUIThread 上。有人知道为什么这不起作用吗?
您忘记调用 .start():
new Thread() { // last BreakPoint stop here
//
}.start;
^^^^^^^
我会将 Thread 加载到变量 t 中,然后您可以 运行 它的成员方法,如 start()、join()、sleep、yield、interrupt 等。
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();