Continue after finished execution ()

0

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

When the data is submitted in the form and you press 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();
}

Request_Login class:

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 this code is returning a java.lang.NullPointerException error in the LOGIN_RESPONSE variable which means that I am doing if(LOGIN_RESPONSE.equals("true")) validation before storing the response at the end of new Request_Login().execute(); execution. p>

How can I do just to validate the variable LOGIN_RESPONSE after finishing the execution of the Request_Login class?

    
asked by anonymous 20.11.2014 / 17:08

1 answer

1

As this is an asynchronous request, the answer you will get is at the end of the cycle, and as you said right after execute() you will not get the result right away.

The end of the loop and the response you expect happens in the onPostExecute method, so you can do it directly on it. If toast is your only action, it gets easier this 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 in the class constructor and store in a private variable.

This is the simplest method, but you can also create an interface to be your listener and implement next to your class Request_Login and stay "listening" the answer in Activity itself. It's up to you.

    
20.11.2014 / 17:36