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 楼答案
来自Android文档:
这是传递的第一个类型。这允许您使用
execute
方法传递某些内容它作为参数传递给
onProgressUpdate
方法这是从
doInBackground
方法传递到onPostExecute
的值onPostExecute
将始终具有返回类型的void如果您的另一个方法在等待
AsyncTask
返回值时阻塞了UI线程,那么它将否定AsyncTask
的使用