java - Android - 从 AsyncTask doInBackground 函数返回整数值

标签 java android android-asynctask sharedpreferences

已解决 谢谢你们,我通过从 AsyncTask 函数中删除 PreExecute 函数解决了我的问题。谢谢大家。

我想从 AsycnTask 函数返回 Integer 值,即 userId 到其他 Activity ,我尝试用谷歌搜索我的问题,并在 stackoverflow 上看到很多讨论,但没有解决我的问题...希望你们能指导我如何做这个。

最终代码:(解决问题后)

private class ValidateUserAccount extends AsyncTask<Void, Integer, Void> {
    String response = "";

    @Override
    protected Void doInBackground(Void... voids) {
        HashMap<String, String> loginDataParams = new HashMap<>();
        loginDataParams.put("email", strEmail);
        loginDataParams.put("password", strPassword);
        JSONObject jsonData = new JSONObject(loginDataParams);
        String path = "myUrl";
        response = Login.ServerData(path, jsonData);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        uProgressBar.setVisibility(View.GONE);
        int code = Login.passResCode();
        userId = Login.passUserId();
        if (code == 200) {
            Toast.makeText(getApplicationContext(), "Successfully Login: " + userId, Toast.LENGTH_SHORT).show();
            userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
            Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class);
            userIdEditor = userIdSharedPreferences.edit();
            userIdEditor.putInt("userId", userId);
            userIdEditor.apply();
            startActivity(loginSuccess);
            finish();
        } else {
            final AlertDialog.Builder alertBox = new AlertDialog.Builder(LoginActivity.this);
            String errorMessage = "";
            if (code == 401)
                errorMessage = "Email and Password are not valid.";
            else if (code == 0)
                errorMessage = "Please check your internet connection.";

            alertBox.setMessage(errorMessage).setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.cancel();
                        }
                    });
            AlertDialog alert = alertBox.create();
            alert.setTitle("Oops, Something went wrong!");
            alert.show();
        }
    }
}

最佳答案

我认为你可以把它作为额外的 Intent ......我还没有真正理解你为什么使用 userIdEditor 以及它是什么......但我认为这样的东西会起作用

Intent 登录成功 = new Intent(getApplicationContext(), MainActivity.class); loginSuccess.putExtra("userId",userId); startActivity(登录成功); 你可以通过执行以下操作在打开的 Activity 中取回 id

int userId = getIntent().getExtra().getInt("userId");

关于java - Android - 从 AsyncTask doInBackground 函数返回整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46340262/

相关文章:

android - float 操作按钮显示为灰色框

android - 类似模式的 SQL 接口(interface)?

java - 如何从 onClick Java 调用类

java - 如何使用 selenium webdriver 截取当前窗口的屏幕截图或模拟打印屏幕

java - spring 内部是否使用 servlet?

java - 使用超时异常时java中的计时器问题,代码在超时异常后无法正确运行

java - 在后台加载数据,更新适配器?

java - JDBC Clob 和 NClob

android:layout_centerInParent/RelativeLayout

android - 如何在前台服务中将多个任务排队,以便它们一项一项地执行?