doinBackground无法保存字段值

时间:2017-10-03 10:14:09

标签: android android-asynctask

我试图找出原因doInBackground()无法保存字段值。返回值后,返回值更改回初始值。我在主类中初始化了AsyncTask onCreate()。一切正常,直到onPostExecute()。 提前致谢

private class UserRegisterTask extends AsyncTask<Void, Void, Boolean> {
    JSONObject jsonObj;
    String uuid;
    String ok;
    String errorMessage;
    Boolean noErrors = false;


    public UserRegisterTask() {

    }

这是doInBack ...

@Override
    protected Boolean doInBackground(Void... params) {

        final String url = "https://webaddress/register.php";
        final Context context = getContext();

        // Request a string response from the provided URL.

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {
                        try {
                            jsonObj = new JSONObject(response);

                            String error = jsonObj.getString("error");
                            if (error.equals("false")) {
                                uuid = jsonObj.getString("unique_id");

                                noErrors = true;
                                Log.e("####¤%", String.valueOf(noErrors.booleanValue()));
                                mEmail = jsonObj.getString("email");
                                ok = jsonObj.getString("ok");

                            } else {
                                errorMessage = jsonObj.getString("error_msg");
                                Log.d("XXXXXXXXXXXXX", errorMessage);
                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //mTextView.setText("That didn't work!");
                Log.e("ERROR", error.getMessage());

                error.printStackTrace();

            }
        })

        {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("password", mpassword);
                params.put("email", mEmail);
                return params;
            }

        };

        AppController.getInstance().addToRequestQueue(stringRequest);

        return noErrors;
    }

这是onPostExecute()

@Override
    protected void onPostExecute(Boolean success) {
        urt = null;
        if (success.booleanValue()) {
            Toast toast = Toast.makeText(getApplicationContext(), ok, Toast.LENGTH_LONG);
            toast.show();

            new Thread(new Runnable() {

                public void run() {

                    new SendEmail(mEmail, sb.toString(), uuid);
                }
            }).start();

            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            intent.putExtra("email", mEmail);
            startActivity(intent);

            // finish();
        } else {
            Toast toast = Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_SHORT);
            toast.show();
            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(intent);
        }




    }

0 个答案:

没有答案