我如何在Android中等待OnPostExecute完成?
我试图执行我如何在Android中等待OnPostExecute完成?,android,android-asynctask,Android,Android Asynctask,我试图执行AsyncTask但当AsyncTask启动并doInBackground完成(返回值)时,它跳过OnPostExecute并运行下面的代码requestTask2.execute(),然后再更改OnPostExecute中的值,它试图在条件的情况下运行,因此我得到空值 让我用代码解释一下: public void onClick(DialogInterface dialog,int id) { Intent gt = new Intent
AsyncTask
但当AsyncTask
启动并doInBackground
完成(返回值)时,它跳过OnPostExecute
并运行下面的代码requestTask2.execute()
,然后再更改OnPostExecute
中的值,它试图在条件的情况下运行,因此我得到空值
让我用代码解释一下:
public void onClick(DialogInterface dialog,int id) {
Intent gt = new Intent(MainActivity.this, favorite.class);
String password = userInput.getText().toString();
String kadi = userInput2.getText().toString();
RequestTask2 requestTask2 = new RequestTask2();
requestTask2.execute("http://www.example.com/androfav/?fav2="+kadi+":"+password).get();
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
// This if condition works before on Post Excecute and it is causing the problem.
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}}
class RequestTask2 extends AsyncTask<String, String, String> {
private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.setMessage("Diziler Yükleniyor \n Lütfen Bekleyin...");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected String doInBackground(String... uri2) {
HttpClient httpclient2 = new DefaultHttpClient();
HttpResponse response2;
String responseString2 = null;
try {
response2 = httpclient2.execute(new HttpGet(uri2[0]));
StatusLine statusLine2 = response2.getStatusLine();
if (statusLine2.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response2.getEntity().writeTo(out);
out.close();
responseString2 = out.toString();
} else {
// Closes the connection.
response2.getEntity().getContent().close();
throw new IOException(statusLine2.getReasonPhrase());
}
} catch (ClientProtocolException e) {
// TODO Handle problems..
} catch (IOException e) {
// TODO Handle problems..
}
return responseString2;
}
@Override
protected void onPostExecute(String result2) {
super.onPostExecute(result2);
try {
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
//............................... Codes
dialog.dismiss();
}
}
public void onClick(DialogInterface对话框,int-id){
Intent gt=新的Intent(MainActivity.this、favorite.class);
字符串密码=userInput.getText().toString();
字符串kadi=userInput2.getText().toString();
RequestTask2 RequestTask2=新的RequestTask2();
requestTask2.execute(“http://www.example.com/androfav/?fav2=“+kadi+”:“+password).get();
if(asd2[0][0]!=null&&asd2[1][0]!=null){
//如果此条件在执行“on Post EXECUTE”之前起作用,则会导致问题。
if(asd2[0][0].equals(password)&&asd2[1][0].endsWith(kadi)){
//代码
}}
类RequestTask2扩展了AsyncTask{
private ProgressDialog=新建ProgressDialog(MainActivity.this);
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
setMessage(“Diziler Yükleniyor\n Lütfen Bekleyin…”);
dialog.show();
对话框。可设置可取消(false);
}
@凌驾
受保护的字符串DOIN背景(字符串…uri2){
HttpClient httpclient2=新的默认HttpClient();
HttpResponse响应2;
字符串responseString2=null;
试一试{
response2=httpclient2.execute(新的HttpGet(uri2[0]);
StatusLine statusLine2=response2.getStatusLine();
if(statusLine2.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response2.getEntity().writeTo(out);
out.close();
responseString2=out.toString();
}否则{
//关闭连接。
response2.getEntity().getContent().close();
抛出新IOException(statusLine2.getReasonPhrase());
}
}捕获(客户端协议例外e){
//处理问题。。
}捕获(IOE异常){
//处理问题。。
}
返回响应2;
}
@凌驾
受保护的void onPostExecute(字符串结果2){
super.onPostExecute(result2);
试一试{
JSONArray jsonResponse2=新的JSONArray(结果2);
asd2=新字符串[3][jsonResponse2.length()];
//编码
dialog.dismise();
}
}
如果
条件起作用,我如何等待OnPostExecute
,然后再执行。
希望我能理解我自己。
提前感谢。异步任务顾名思义是异步的。您需要将if条件移动到onPostExecute
将下面的命令移动到onPostExecute
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}
}
@Override
protected void onPostExecute(String result2) {
super.onPostExecute(result2);
try {
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}
}
}
dialog.dismiss();
}
编辑:
我没有注意到您调用了get()
。调用get()
使Asynctask不再是异步的。您永远不应该调用get()
只调用execute()
就够了
为什么需要调用get()
,这会阻止等待任务完成的ui线程。在使用AsyncTask
时,应始终避免调用get()
。相反,应在onPostExecute
中执行所有后处理
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}
}
@Override
protected void onPostExecute(String result2) {
super.onPostExecute(result2);
try {
JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {
if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) {
// Codes
}
}
}
dialog.dismiss();
}
非常感谢您的回答。我不知道我怎么看不到这一点。它运行得很好。谢谢。@user3583237不客气。我认为您误解了asynctask的使用,导致了这一错误。是的,我确实误解了AsynTask过程,但现在我明白了。再次感谢您。是的,您完全正确,我正在尝试等待AsyncTask,我认为它可能有效,但没有。谢谢,你说得很好。不需要使用get()
。