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?