【问题标题】:Android - Intent data after AsyncTaskAndroid - AsyncTask 之后的 Intent 数据
【发布时间】:2021-07-10 08:22:55
【问题描述】:

服务器连接正在后台 (AsyncTask) 和服务器响应 JSON 上工作。 我想将数据从服务器发送到其他活动,但它不工作。 我试图解决这个问题,但没有任何效果。我该如何解决这个问题? 我的代码如下:

public class LoginRequest extends AsyncTask<Void, Void, String> {

    String errorMsg = LOGIN_ERROR;
    String builder;

    @Override
    protected String doInBackground(Void... voids) {

        JSONObject requestJsonObject = new JSONObject();
        try {

            requestJsonObject.put("email", userEmail);
            requestJsonObject.put("password", userPassword);
        } catch (JSONException e) {

            e.printStackTrace();
        }

        requestQueue = Volley.newRequestQueue(getApplicationContext());

        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST,
                BASE_URL,
                requestJsonObject,
                response -> {
                    errorMsg = LOGIN_SUCCESS;
                    Log.d(TAG, "response = " + response);
                    builder = response.toString();
                },
                error -> errorMsg = LOGIN_ERROR
        );

        requestQueue.add(request);
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        if (errorMsg.equals(LOGIN_ERROR)) {

            textViewError.setText(R.string.login_failure);
        } else {

            Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();
            // Send data from server to UserMainActivity
            Intent intent = new Intent(LoginActivity.this, UserMainActivity.class);
            intent.putExtra("serverMessage", builder);
            startActivity(intent);
        }
    }
}

【问题讨论】:

  • 尝试删除 super.onPostExecute(s);
  • 不,不要像@Shayan所说的那样删除super.onPostExecute(s)
  • 你所做的并没有错。以最短的方式发布您的 json 响应,并向我们展示您如何处理 UserMainActivity 中传递的变量。
  • @ZankrutParmar 我在响应侦听器中将 JSON 数据从服务器更改为字符串,并将 UserMainActivity 中的数据作为 Intent dataIntent = getIntent(); serverMessage = dataIntent.getStringExtra(SERVER_MESSAGE);
  • serverMessage 只是字符串变量。

标签: android server android-asynctask


【解决方案1】:

您应该将您的活动上下文传递给您的 asyncTask 并从该上下文启动活动

您的 AsyncTask 应如下所示

private class LoginRequest extends AsyncTask<Void, Void, String> {


    Context context;
    LoginRequest(Context context)
    {
        this.context=context;
    }


    String builder;

    @Override
    protected String doInBackground(Void... voids) {

        //your code here

        return null;
    }

    @Override
    protected void onPostExecute(String s) { 

        //your code here

        Intent intent = new Intent(context, NewActivity.class);
        intent.putExtra("serverMessage", builder);
        context.startActivity(intent);
    }
}

像这样调用这个 asyncTask

    LoginRequest asyncTaskForceUpdate=new LoginRequest(this);
    asyncTaskForceUpdate.execute();

【讨论】:

  • 标志来检查登录是否成功?在响应侦听器中,我将 errorMsg 变量更改为 SUCCESS,错误侦听器将 errorMsg 变量更改为 FAIL,但是在 Method onPostExecute 中,出现了一个错误,类似于 Attempt to invoke virtual method 'boolean java.lang.String.equals(java. lang.Object)' 在空对象引用上
【解决方案2】:

尝试如下更改您的代码:

public class LoginRequest extends AsyncTask<Void, Void, String> {

    String errorMsg = "ERROR";
    String builder = "";

    @Override
    protected String doInBackground(Void... voids) {

        JSONObject requestJsonObject = new JSONObject();
        try {

            requestJsonObject.put("email", userEmail);
            requestJsonObject.put("password", userPassword);
        } catch (JSONException e) {

            e.printStackTrace();
        }

        requestQueue = Volley.newRequestQueue(getApplicationContext());

        JsonObjectRequest request = new JsonObjectRequest(
                Request.Method.POST,
                BASE_URL,
                requestJsonObject,
                response -> {
                    errorMsg = "SUCCESS";
                    Log.d(TAG, "response = " + response);
                    builder = response.toString();
                },
                error -> errorMsg = "ERROR"
        );

        requestQueue.add(request);
        return errorMsg;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        if (s.equals("ERROR")) {

            textViewError.setText(R.string.login_failure);
        } else {

            Toast.makeText(getApplicationContext(), "Login Success", Toast.LENGTH_SHORT).show();
            // Send data from server to UserMainActivity
            Intent intent = new Intent(LoginActivity.this, UserMainActivity.class);
            intent.putExtra("serverMessage", builder);
            startActivity(intent);
        }
    }
}

我将doInBackground 的返回值更改为errorMsg。因为这是更常见的做法。告诉我这是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多