Issue
I have an asynctask
that is getting a userPin from my oracle database. In my Main thread then on a button I am running the asynctask
. The value from the database is assigned to a variable called userPinRetrieved
.
When I debug this variable it is receiving the correct value. However, when I am running the app normally it is receiving null. After doing some research and using Thread.Sleep(x)
I can see that is due to the fact that the asynctask is not returning the result to the main thread and the variable in time.
I have been advised not to use Thread.Sleep(x)
, what alternatives do I have?
Here is my code:
AsyncTask:
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((line=br.readLine()) != null) {
JSONObject json = new JSONObject(line);
Log.d("Line",line);
if (json.getString("userPin") == null){
userPinRetrieved = "PIN NOT RECEIVED";
Log.d("userpin", userPinRetrieved);
} else {
userPinRetrieved = json.getString("userPin");
Log.d("userpin", userPinRetrieved);
}
}
}
} catch (Exception e) {
Log.v("ErrorAPP", e.toString());
}
return "";
}
@Override
protected void onPostExecute(String userPinRetrieved) {
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
}
Sign-In Button:
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner postReq = new AsyncTaskRunner();
postReq.execute("start");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Toast.makeText(UserLogin.this, userPinRetrieved + " " + userPin, Toast.LENGTH_LONG).show();
if (userPin.equals(userPinRetrieved)) {
Toast.makeText(UserLogin.this, "Access Granted!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Toast.makeText(UserLogin.this, "Hello " + employee, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UserLogin.this, "Access Denied! Incorrect Pin", Toast.LENGTH_SHORT).show();
}
}
});
Can anyone offer suggestions?
Thanks
Solution
The point of AsyncTask
is to move the processing to another thread because you don't know how much time it will take to run a certain task. You can process the result of the async task inside the onPostExecute
method.
So move the usage of the result to there:
@Override
protected void onPostExecute(String userPinRetrieved) {
if (userPin.equals(userPinRetrieved)) {
Toast.makeText(UserLogin.this, "Access Granted!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Toast.makeText(UserLogin.this, "Hello " + employee, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UserLogin.this, "Access Denied! Incorrect Pin", Toast.LENGTH_SHORT).show();
}
}
You can also define a method that will be called when the processing is finished:
protected void onPostExecute(String userPinRetrieved) {
processValue(userPinRetrieved);
}
You need to be careful when referencing variables from your activity in the AsyncTask, if the activity gets destroyed during the processing of your AsyncTask it can cause a memory leak.
Answered By - Levi Moreira
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.