有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AsyncTask未按预期返回布尔值

老实说,异步任务是有史以来最烦人的方法,我从来都不习惯它的功能

这是我的任务代码:

public class myAsync extends AsyncTask<String, Boolean, Boolean>{

boolean thisisresult=false;



         @Override
            protected Boolean doInBackground(String...urls){    


  try {
        socket = new Socket(server, port);
   }

   catch(Exception ec) {

    Log.i(LOGTAG,"Error connectiong to server:" + ec);
    return false;
  }
   String msg = "Connection accepted " + socket.getInetAddress() + ":" +  
socket.getPort();

   Log.i(LOGTAG, msg);

    try
    {
        sInput  = new ObjectInputStream(socket.getInputStream());
        sOutput = new ObjectOutputStream(socket.getOutputStream());
    }
    catch (IOException eIO) {

        Log.i(LOGTAG,"Exception creating new Input/output Streams: " +   
eIO);
        return false;
    }

    try
    {




            EditText  u=(EditText)findViewById(R.id.inputuser);
            theuser = u.getText().toString();
            EditText p =(EditText)findViewById(R.id.inputpassword);
            thepass = p.getText().toString();
            EditText n =(EditText)findViewById(R.id.realname);
            thename = n.getText().toString();
            EditText m =(EditText)findViewById(R.id.inputmail);
            themail = m.getText().toString();
            EditText ph = (EditText)findViewById(R.id.inputphone);
            thephone = ph.getText().toString();





        int RID=RandomNumbers.Rgenerate();
        String newRID = Integer.toString(RID);


        sOutput.writeObject(theuser);
        sOutput.writeObject(thepass);
        sOutput.writeObject(thename);
        sOutput.writeObject(themail);
        sOutput.writeObject(thephone);
        sOutput.writeObject(newRID);
        sOutput.flush();
                Log.i(LOGTAG,"this"+theuser + thepass + thephone +   

themail + thename+ newRID);

        String REresponse = (String) sInput.readObject();


while(recieved==false)
{

if (REresponse.equals("('T','"+newRID+"')")){
        Log.i(LOGTAG,"you made it");
//  showthis=("اکانت با موفقیت ساخته شد");
    recieved = true;
    thisisresult=true;
}else if (REresponse.equals("('F','"+newRID+"')")){
        Log.i(LOGTAG,"you failed");
    //  showthis=("username تکراری است");
    recieved=true;
    thisisresult=true;
}else{

    //  showthis=("جوابی از سرور دریافت نشد");
        Log.i(LOGTAG,REresponse);
        Log.i(LOGTAG,"some thing is wrong");
            }
        }
    }


    catch (IOException eIO) {
//          display("Exception doing login : " + eIO);
        Log.i(LOGTAG,"Exception doing login : " + eIO);
        disconnect();


    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // success we inform the caller that it worked
    return true;
}

 }

protected void onPostExecute(Boolean result){
TextView tv = (TextView) findViewById(R.id.alerttext);

if(result==true){
    tv.setText("done");

}
else{
    tv.setText("Not done");
}

底部my async应该返回布尔值true,因为我已经将其设置为true,但它没有,有人掌握了这个方法来帮助我吗


共 (1) 个答案

  1. # 1 楼答案

    来自Android文档:

    The three types used by an asynchronous task are the following:

    • Params, the type of the parameters sent to the task upon execution.

    这是传递的第一个类型。这允许您使用execute方法传递某些内容

    • Progress, the type of the progress units published during the background computation.

    它作为参数传递给onProgressUpdate方法

    • Result, the type of the result of the background computation.

    这是从doInBackground方法传递到onPostExecute的值

    onPostExecute将始终具有返回类型的void

    如果您的另一个方法在等待AsyncTask返回值时阻塞了UI线程,那么它将否定AsyncTask的使用