从内部类(AsyncTask)访问私有变量



我有一个问题,我试图从AsyncTaskdoInBackground方法访问外部类的私有变量,这是内部类。这里有完整的代码。

public class MessageHandler {
private Contact receiver;
private String senderFacebookId;
private String message;
private final String TAG = "MessageHandler";
private Context context;
boolean sentResult;
public MessageHandler(Contact receiver, String senderFacebookId, String message, Context context) {
    this.receiver = receiver;
    this.senderFacebookId = senderFacebookId;
    this.message = message;
    this.context=context;
    this.sentResult = false;
}
public void send(){
    Log.i(TAG, "Sending message to " + receiver.getName() + " receiver id: " + receiver.getFacebook_id() + " sender id: " + senderFacebookId + " message: " + message);
    new SenderAsync().execute(senderFacebookId,message, receiver.getFacebook_id());
    if(this.sentResult==true){
        Toast toast = Toast.makeText(this.context, "Message Sent", Toast.LENGTH_LONG);
        toast.show();
    }else{
        Toast toast = Toast.makeText(this.context, "ERROR: Message not Sent", Toast.LENGTH_LONG);
        toast.show();
    }
}

private class SenderAsync extends AsyncTask<String,String,String> {
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
    @Override
    protected String doInBackground(String... params) {
        HttpClient client = new DefaultHttpClient();
        try {
            List<NameValuePair> parameter = new ArrayList<>(1);
            parameter.add(new BasicNameValuePair("regId", "empty"));
            parameter.add(new BasicNameValuePair("sender_id", params[0]));
            parameter.add(new BasicNameValuePair("receiver_facebook_id", params[2]));
            parameter.add(new BasicNameValuePair("message", params[1]));
            String paramString = URLEncodedUtils.format(parameter, "utf-8");
            HttpGet get = new HttpGet(ProfileFragment.SERVER_URL_SEND_MESSAGE+"?"+paramString);
            Log.i(TAG, paramString);
            HttpResponse resp = client.execute(get);
            System.out.println("SREVER RESPONSE " + resp.getStatusLine().getStatusCode());
            //THIS IS THE VARIABLE THAT I WANT TO ACCESS
            if(resp.getStatusLine().getStatusCode()==200){
                MessageHandler.this.sentResult = true;
            }

        } catch (IOException e) {
            Log.i(TAG,"Error :" + e.getMessage());
        }
        return null;
    }
}

}

我试图访问的变量是布尔变量sentResult。我还打印出服务器的响应,它总是200。即使我删除了if条件并在任何情况下将其设置为true,看起来那行代码永远不会执行,变量也不会被访问,所以它总是false

异步任务的全部意义在于它是异步的。如果执行一个任务,在下一行代码中不会得到结果。为了评估结果,您应该使用onPostExecute回调方法。

@Override
protected void onPostExecute(String s) {
    if(sentResult==true){
        Toast toast = Toast.makeText(this.context, "Message Sent", Toast.LENGTH_LONG);
        toast.show();
    }else{
        Toast toast = Toast.makeText(this.context, "ERROR: Message not Sent", Toast.LENGTH_LONG);
        toast.show();
    }
}

同样,如果你要改变你的方法签名,你甚至不需要一个外部变量。

private class SenderAsync extends AsyncTask<String,String,Boolean> {

@Override
protected void onPostExecute(Boolean sent) {
    if(sent == true){
        Toast toast = Toast.makeText(this.context, "Message Sent", Toast.LENGTH_LONG);
        toast.show();
    }else{
        Toast toast = Toast.makeText(this.context, "ERROR: Message not Sent", Toast.LENGTH_LONG);
        toast.show();
    }
}
@Override
protected Boolean doInBackground(String... params) {
    try {
        ...
        if (resp.getStatusLine().getStatusCode() == 200) {
            return true;
        }
    }
    catch (IOException e) {
    }
    return false;
}

您正在尝试验证sentResult后,你告诉AsyncTask做它的工作。AsyncTask在一个单独的线程上并行地做它必须做的事情。因此,直到它完成,sentResult是不变的。

我的建议是把你的Toast逻辑放在onPostExecute.

为SenderAsync创建构造函数;

public SenderAsync(Context Context, String senderId, String msg, Contact receiver)

这样称呼它;

new SenderAsync(senderFacebookId,message, receiver.getFacebook_id()).execute();

你需要在onPostExecute中得到结果。onPostExecute(字符串返回值){//在这里运行if语句}

您正在错误地使用AsyncTask。AsyncTask应该在onPostExecute()中返回结果,你的UI应该有一个单独的方法onPostExecute()调用(因此异步名称)。对于你想要完成的任务,你应该这样做:

public void send() {
    new SenderAsync().execute(senderFacebookId,message, receiver.getFacebook_id());
}
public void processResult(boolean result) {
  String resultMsg = result ? "Message Sent" : "ERROR: Message not Sent";
  Toast.makeText(this.context, resultMsg, Toast.LENGTH_LONG).show();
}
private class SenderAsync extends AsyncTask<String, String, Boolean> {
  ...
  @Override
    protected String doInBackground(String... params) {
      ...
      try {
        ...
        return (resp.getStatusLine().getStatusCode() == 200)
      }
      ...
      return false;
    }
    @Override
    protected void onPostExecute(Boolean result) {
        processResult(result);
    }
}

相关内容

  • 没有找到相关文章

最新更新