Continue after run() finished

Asked

Viewed 50 times

0

I have a class with the name Request_Login validates the login data with the server and a string LOGIN_RESPONSE which stores the server response.

When data is submitted in the form and pressed enter the following code is executed:

// VALIDATE LOGIN
new Request_Login().execute();

if(LOGIN_RESPONSE.equals("true")){
    Toast.makeText(getApplicationContext(), "Login efectuado com sucesso", Toast.LENGTH_SHORT).show();

}else{
    Toast.makeText(getApplicationContext(), "Login sem sucesso.\nValide os seus dados e tente novamente.", Toast.LENGTH_SHORT).show();
}

Class Request_login:

class Request_Login extends AsyncTask<String,String,String> {

    @Override
    protected String doInBackground(String... string) {

        String RETRIEVED_CONTENT = "";

        InputStream inputStream;

        try {
            // New HTTP Object
            HttpClient httpclient = new DefaultHttpClient();

            // Build POST Array
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email", user_input.toString()));
            nameValuePairs.add(new BasicNameValuePair("password", pass_input.toString()));

            // New POST Object
            HttpPost httppost = new HttpPost(DOMAIN+SYS_PATH+file);


            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);

            StringBuilder sb = new StringBuilder();
            String line;
            while((line = reader.readLine()) != null){
                sb.append(line).append("\n");
            }

            RETRIEVED_CONTENT = sb.toString();

            Log.d("HTTP", "HTTP: OK");
        } catch (Exception e) {
            Log.e("HTTP", "Error in http connection " + e.toString());
        }

        LOGIN_RESPONSE = RETRIEVED_CONTENT;
        Log.i("RETRIEVED_CONTENT", RETRIEVED_CONTENT);
        return RETRIEVED_CONTENT;
    }

    @Override
    protected void onPreExecute() {
        progressDialog= ProgressDialog.show(Login.this, "Por favor aguarde...","Validando dados de login.", true);
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
    }



}

The problem is that that code is returning an error java.lang.NullPointerException in the variable LOGIN_RESPONSE which means I’m doing the validation if(LOGIN_RESPONSE.equals("true")) before it stores the answer at the end of the new Request_Login().execute();.

How can I only validate the variable LOGIN_RESPONSE after the execution of the class Request_Login?

  • If possible, also put the class Request_Login to help answer. This class is an extension of AsyncTask?

  • 1

    http://meta.pt.stackoverflow.com/a/1911/101

  • @Paulorodrigues added the Request_login class

1 answer

1

This being an asynchronous request, the answer you will get is at the end of the cycle, and as you well said, right after the execute() you will not get the result immediately.

The end of the cycle and the response you expect happens in the method onPostExecute, then you can do directly on it. If the Toast for your only action, it’s easier that way:

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    progressDialog.dismiss();

    if (result.equals("true")) {
        Toast.makeText(context, "Login efectuado com sucesso", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(context, "Login sem sucesso.\nValide os seus dados e tente novamente.", Toast.LENGTH_SHORT).show();
    }
}

The context you can pass the class constructor and store in a private variable.

This is the simplest method, but you can also create a interface to be his Listener and implement with your class Request_Login and be "listening" to the answer itself Activity. At your discretion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.