gpt4 book ai didi

android - AsyncTask、HttpClient 和 ProgressDialog

转载 作者:太空狗 更新时间:2023-10-29 16:08:15 26 4
gpt4 key购买 nike

我正在创建一个 AsyncTask 来将用户登录到服务器。登录工作正常,但 ProgressDialog 直到过程结束才显示。一旦用户点击按钮,UI 就会卡住,并且我的对话框不会显示。

我很感激任何帮助。这是我的代码。

public class MyApp extends Activity {
private ProgressDialog dialogo = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button loginButton = (Button) findViewById(R.id.btnLogin);
loginButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
SharedPreferences preferencias = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String webAddress = preferencias.getString("webAddress", "");

if (webAddress.isEmpty()) {
Toast.makeText(getBaseContext(), "Please, configure a Web Address.", Toast.LENGTH_LONG).show();
} else {
EditText edtUsername = (EditText) findViewById(R.id.edtUsername);
EditText edtPassword = (EditText) findViewById(R.id.edtPassword);

HashMap<String, String> parametros = new HashMap<String, String>();
parametros.put("username", edtUsername.getText().toString());
parametros.put("password", edtPassword.getText().toString());

Requisicao requisicao = new Requisicao(parametros);
AsyncTask<String, Void, String> resposta = requisicao.execute(webAddress + "/login");

try {
Toast.makeText(getBaseContext(), resposta.get(), Toast.LENGTH_LONG).show();
} catch (InterruptedException e) {
Toast.makeText(getBaseContext(), "InterruptedException (login)", Toast.LENGTH_LONG).show();
} catch (ExecutionException e) {
Toast.makeText(getBaseContext(), "ExecutionException (login)", Toast.LENGTH_LONG).show();
}
}
}
});

ImageView engrenagem = (ImageView) findViewById(R.id.imgEngrenagem);
engrenagem.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Intent preferenciasActivity = new Intent(getBaseContext(), Preferencias.class);
startActivity(preferenciasActivity);
}
});
}



public class Requisicao extends AsyncTask<String, Void, String> {
private final HttpClient clienteHttp = new DefaultHttpClient();
private String resposta;
private HashMap<String, String> parametros = null;

public Requisicao(HashMap<String, String> params) {
parametros = params;
}

@Override
protected void onPreExecute() {
dialogo = new ProgressDialog(MyApp.this);
dialogo.setMessage("Aguarde...");
dialogo.setTitle("Comunicando com o servidor");
dialogo.setIndeterminate(true);
dialogo.setCancelable(false);
dialogo.show();
}

@Override
protected String doInBackground(String... urls) {
byte[] resultado = null;
HttpPost post = new HttpPost(urls[0]);
try {
ArrayList<NameValuePair> paresNomeValor = new ArrayList<NameValuePair>();
Iterator<String> iterator = parametros.keySet().iterator();
while (iterator.hasNext()) {
String chave = iterator.next();
paresNomeValor.add(new BasicNameValuePair(chave, parametros.get(chave)));
}

post.setEntity(new UrlEncodedFormEntity(paresNomeValor, "UTF-8"));

HttpResponse respostaRequisicao = clienteHttp.execute(post);
StatusLine statusRequisicao = respostaRequisicao.getStatusLine();
if (statusRequisicao.getStatusCode() == HttpURLConnection.HTTP_OK) {
resultado = EntityUtils.toByteArray(respostaRequisicao.getEntity());
resposta = new String(resultado, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
} catch (Exception e) {
}
return resposta;
}

@Override
protected void onPostExecute(String param) {
dialogo.dismiss();
}
}
}

最佳答案

尝试注释掉按钮监听器中的 resposta.get() 调用。我猜它只会阻塞主 UI 线程,直到任务完成。

关于android - AsyncTask、HttpClient 和 ProgressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9036127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com