1

I have made an application where in the starting we ask for username and security password and then let the user use the application.

Currently, my application asks for username and security question one after the other in a pop out box but in the background the application also runs.

The application is waiting for the user to enter the username and security question answer because of which post url request fails to get a request because by that time "ans1[0]" variable is not set.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    securityquestion(); // **This part calls the pop up dialog boxes** 
    statusTxtView = (TextView) findViewById(R.id.status_text);
    //Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
    intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
    intentFilter
            .addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
    channel = manager.initialize(this, getMainLooper(), null);
   // super.onPause();
    startRegistrationAndDiscovery();
    //super.onResume();
    servicesList = new WiFiDirectServicesList();
    getFragmentManager().beginTransaction()
            .add(R.id.container_root, servicesList, "services").commit();

}

    public void securityquestion(){
        final EditText txtUrl = new EditText(this);
        final EditText txtUrl2 = new EditText(this);
        final String[] ans1 = {""};
        final String[] ans2 = {""};
// Set the default text to a link of the Queen
        int num = getradomquestionumber();
        String messge = "";
        if(num==1)
        {
            txtUrl.setHint("First Pet name");
            messge = "What is your first pet name?";
        }else{
            txtUrl.setHint("Mother's maiden name");
            messge = "What is your mother's maiden name?";
        }
        txtUrl2.setHint("User Name");


        new AlertDialog.Builder(this)
                .setTitle("Security Question")
                .setMessage(messge)
                .setView(txtUrl)
                .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String url = txtUrl.getText().toString();
                        ans2[0] = url;
                        // moustachify(null, url);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .show();

        new AlertDialog.Builder(this)
                .setTitle("User Name")
                .setMessage("Enter UserName")
                .setView(txtUrl2)
                .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        String url = txtUrl.getText().toString();
                        ans1[0] = url;
                        // moustachify(null, url);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
                .show();

        HashMap<String , String> postDataParams = new HashMap<String, String>();
        postDataParams.put("key", String.valueOf(num));
        postDataParams.put("key2", ans1[0]);
        response = performPostCall("http://10.19.23.2/NEWS/SecurityQuestion.php", postDataParams);
        System.out.println("response -------------- "+response);
        if(response == ans2[0]){

        }else{
            System.exit(0);
        }
    }

Currently I am unable to get a response from the server because the application doesn't wait for the response from the user. How do I pause the application so that fir the dialog box appears first, takes users input and then further after authentication, only continue the application.

Requesting you to please help me with your suggestions.

Alia
  • 13
  • 5

2 Answers2

0

You need to do something like this:

  1. Your main page is created (initialize what needs) and is started
  2. Your main page opens the pop up dialog
  3. User type in name and question, click something
  4. At this point, you make the server request (on background). Here your main page or pop up must show some indicator that it's waiting, like a spinner
  5. If server is successful, you stop spinning and continue
  6. If server is unsuccessful, you stop spinning, print some error to the user, and let her try again

Also, execution code shouldn't be done in onCreate(), but in onStart() and then whenever the server is back to you with a positive answer. See activity lifecycle for this really basic stuffs

Alessio
  • 3,063
  • 1
  • 15
  • 17
0

Try this by change method securityquestion() like below:

public void securityquestion(){
    final EditText txtUrl = new EditText(this);
    final EditText txtUrl2 = new EditText(this);
    final String[] ans1 = {""};
    final String[] ans2 = {""};
// Set the default text to a link of the Queen
        final int num = getradomquestionumber();
        String messge = "";
        if(num==1)
        {
            txtUrl.setHint("First Pet name");
            messge = "What is your first pet name?";
        }else{
            txtUrl.setHint("Mother's maiden name");
            messge = "What is your mother's maiden name?";
        }
        txtUrl2.setHint("User Name");
    new AlertDialog.Builder(LoginActivity.this)
            .setTitle("User Name")
            .setMessage("Enter UserName")
            .setView(txtUrl2)
            .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String url = txtUrl2.getText().toString();
                    ans1[0] = url;
                    // moustachify(null, url);

                    //ask question and get result
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("Security Question")
                            .setMessage(messge)
                            .setView(txtUrl)
                            .setPositiveButton("Enter", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    String url = txtUrl.getText().toString();
                                    ans2[0] = url;
                                    // moustachify(null, url);

                                    //Now call or make server request
                                    HashMap<String , String> postDataParams = new HashMap<String, String>();
                                    postDataParams.put("key", String.valueOf(num));
                                    postDataParams.put("key2", ans1[0]);
                                    response = performPostCall("http://10.19.23.2/NEWS/SecurityQuestion.php", postDataParams);
                                    System.out.println("response -------------- "+response);
                                    if(response == ans2[0]){

                                    }else{
                                        System.exit(0);
                                    }
                                }
                            })
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                }
                            })
                            .show();
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            })
            .show();
}
Nitin Patel
  • 1,605
  • 13
  • 31
  • The ideo you gave is using an alert box under another alert box. But you cannot pass this in the later alert box. What should I do ? – Alia Jul 05 '17 at 06:49
  • @Alia Change `this` with whatever Activity.this like (`MainActivity.this`) in both alertdialog and try again. And if app crashes again then check error in `AndroidMonitor` and post it. – Nitin Patel Jul 05 '17 at 06:56
  • W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.huawei.lcagent.client.LogCollectManager.getUserType()' on a null object reference Null point exception – Alia Jul 05 '17 at 07:07
  • @Alia Where you have used `getUserType()` method? – Nitin Patel Jul 05 '17 at 07:12
  • no where have I used that function. Seems it is called form some inner class function. – Alia Jul 05 '17 at 07:17
  • @Alia Can you copy more lines from log-cat? So I'll get more idea – Nitin Patel Jul 05 '17 at 07:19