0

System.out.println("true"); で確認しています。doInBackground で true を返しています。しかし、その後 onPostExecute() は開始されません。私はこれで初心者です。コードは次のとおりです。

    @SuppressWarnings("rawtypes")
private class NetCheck extends AsyncTask {
    private ProgressDialog nDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        nDialog = new ProgressDialog(Register.this);
        nDialog.setMessage("Loading..");
        nDialog.setTitle("Checking Network");
        nDialog.setIndeterminate(false);
        nDialog.setCancelable(true);
        nDialog.show();
    }

    protected Object doInBackground(Object... args) {

        /**
         * Gets current device state and checks for working internet
         * connection by trying Google.
         **/

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            System.out.println("connected");
            try {
                URL url = new URL("http://www.google.com");
                HttpURLConnection urlc = (HttpURLConnection) url
                        .openConnection();
                urlc.setConnectTimeout(3000);
                urlc.connect();
                if (urlc.getResponseCode() == 200) {
                    System.out.println("true");
                    return true;
                }
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("Do in background");
        return false;

    }

    @SuppressWarnings({ "unchecked", "unused" })
    protected void onPostExecute(Boolean th) {
        System.out.println("on post execute");
        if (th == true) {
            nDialog.dismiss();
            new ProcessRegister().execute();
        } else {
            nDialog.dismiss();
            Toast.makeText(getApplicationContext(),
                    "Error in Network Connection", Toast.LENGTH_LONG)
                    .show();
        }
    }
}

@SuppressWarnings("rawtypes")
private class ProcessRegister extends AsyncTask {

    /**
     * Defining Process dialog
     **/
    private ProgressDialog pDialog;

    String email, password, name, password1;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        inputPassword = (EditText) findViewById(R.id.etRegisterPassword);
        inputPassword1 = (EditText) findViewById(R.id.etRegisterConfirmPassword);
        email = inputEmail.getText().toString();
        name = inputName.getText().toString();
        password = inputPassword.getText().toString();
        password1 = inputPassword.getText().toString();
        pDialog = new ProgressDialog(Register.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Registering ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected void onProgressUpdate(Object... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        System.out.println("on progressss");
    }

    protected Object doInBackground(Object... args) {

        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.registerUser(name, email, password);

        return (JSONObject) json;

    }

    @SuppressWarnings("unused")
    protected void onPostExecute(JSONObject json) {
        /**
         * Checks for success message.
         **/
        System.out.println("on posttt execute");
        try {
            if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS);

                String red = json.getString(KEY_ERROR);

                if (Integer.parseInt(res) == 1) {
                    pDialog.setTitle("Getting Data");
                    pDialog.setMessage("Loading Info");

                    registerErrorMsg.setText("Successfully Registered");

                    DatabaseHandler db = new DatabaseHandler(
                            getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    /**
                     * Removes all the previous data in the SQlite database
                     **/

                    UserFunctions logout = new UserFunctions();
                    logout.logoutUser(getApplicationContext());
                    db.addUser(json_user.getString(KEY_NAME),
                            json_user.getString(KEY_EMAIL),
                            json_user.getString(KEY_UID),
                            json_user.getString(KEY_CREATED_AT));
                    /**
                     * Stores registered data in SQlite Database Launch
                     * Registered screen
                     **/

                    Intent registered = new Intent(getApplicationContext(),
                            LogIn.class);

                    /**
                     * Close all views before launching Registered screen
                     **/
                    registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    pDialog.dismiss();
                    startActivity(registered);

                    finish();
                }

                else if (Integer.parseInt(red) == 2) {
                    pDialog.dismiss();
                    Toast.makeText(getApplicationContext(),
                            "User already exists", Toast.LENGTH_LONG)
                            .show();
                } else if (Integer.parseInt(red) == 3) {
                    pDialog.dismiss();
                    Toast.makeText(getApplicationContext(),
                            "Invalid Email id", Toast.LENGTH_LONG).show();
                }

            }

            else {
                pDialog.dismiss();
                Toast.makeText(getApplicationContext(),
                        "Error occured in registration", Toast.LENGTH_LONG)
                        .show();
            }

        } catch (JSONException e) {
            e.printStackTrace();

        }
    }
}

@SuppressWarnings("unchecked")
public void NetAsync(View view) {
    new NetCheck().execute();
}}

これに関する他の質問では答えが見つかりませんでした。

4

1 に答える 1