异步功能无法运行Android

时间:2017-09-27 08:03:37

标签: php android mysql

我希望用户从应用退出后状态应为OFFLINE。 Sp我在注销菜单中写了以下语句 -

else if (id == R.id.nav_lout) {
        status = "Offline";
        mAuthTask = new UserLoginTask();
        mAuthTask.execute((Void) null);

        new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("MyApp")
                .setMessage("Are you sure?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        session.logoutUser();
                    }
                }).setNegativeButton("No", null).show();
    }

现在问题是Alert Box正在显示,用户也已注销。但他的状态在数据库中仍处于在线状态。

status = "Offline";
    mAuthTask = new UserLoginTask();
    mAuthTask.execute((Void) null);

此部分未执行。请帮助。

UserLoginTask类的代码 -

    public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
    UserLoginTask() {
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        InputStream is = null;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("email", uEmailID));
        nameValuePairs.add(new BasicNameValuePair("status", status));

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://mywebsite.com/Android/checkIsOnline.php");
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();

            is = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            result = sb.toString().trim();

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        mAuthTask = null;
    }

    @Override
    protected void onCancelled() {
        mAuthTask = null;
    }
}

3 个答案:

答案 0 :(得分:0)

由于它是异步任务,您需要等待任务的响应才能显示AlertDialog。

我的观点:在您从注销过程获得HTTP 200 OK响应后,在UserLoginTask()内移动警报对话框。

答案 1 :(得分:0)

您应该在单击警报对话框中调用此AsyncTask,将execute()更改为.executeOnExecutor()。然后检查你的onpreexecute和doInBackground是否正常工作。

答案 2 :(得分:0)

在对话框内单击正面按钮时调用此方法。也删除此代码&#34;(Void)null&#34; as byDefault Void本身在asyntask内传递。

 new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("MyApp")
            .setMessage("Are you sure?")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    session.logoutUser();
                    status = "Offline";
                    mAuthTask = new UserLoginTask();
                    mAuthTask.execute();
                }
            }).setNegativeButton("No", null).show();