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 ofAsyncTask
?– Paulo Rodrigues
http://meta.pt.stackoverflow.com/a/1911/101
– Maniero
@Paulorodrigues added the Request_login class
– CIRCLE