AsyncTask没有按预期返回布尔值

用户名

老实说,AsyncTask是迄今为止最令人困扰的方法,我从不习惯它的功能,

这是我的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");
}

我的异步操作的最底层应该返回布尔值true,因为我已将其设置为TRUE,但是事实并非如此,有没有人掌握此方法来帮助我?

184

从Android文档中:

异步任务使用的三种类型如下:

  • 参数,执行时发送给任务的参数类型。

这是第一个通过的类型。这使您可以使用execute方法传递内容

  • 进度,是后台计算过程中发布的进度单位的类型。

这作为参数传递给onProgressUpdate方法

  • 结果,背景计算结果的类型。

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

onPostExecute 返回类型始终为void。

如果您的其他方法在等待的返回值时阻塞了UI线程AsyncTask,则将否定使用AsyncTask

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章