Issue
I'm trying to understand AsyncTask in Android. I can't understand how do we pass parameters. In this code :
protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean> {
String userName = null;
@Override
protected Boolean doInBackground(String... params)
{
RestAPI api = new RestAPI();
boolean userAuth = false;
try
{
JSONObject jsonObj = api.UserAuthentication(params[0], params[1]);
JSONParser parser = new JSONParser();
userAuth = parser.parseUserAuth(jsonObj);
userName = params[0];
}
catch (Exception e)
{
Log.d("AsyncLogin", e.getMessage());
}
return userAuth;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
Toast.makeText(context, "Please wait...", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute(Boolean result)
{
if(result) {
Intent i = new Intent(LoginActivity.this, UserDetailsActivity.class);
i.putExtra("username", userName);
startActivity(i);
}
else
{
Toast.makeText(context, "Not valid username/password", Toast.LENGTH_SHORT).show();
}
}
}
I can't understand why do we use <String, JSONObject, Boolean>
in
protected class AsyncLogin extends AsyncTask<String, JSONObject, Boolean>
What do String, JSONObject and Boolean refer to? Can you explain it to me? Thanks.
Solution
AsyncTask (Type1, Type2, Type3) uses argument types:
- Type1 is the type of argument you pass when you call execute (received in doInBackground)
- Type2 is the type of the argument you pass to onProgressUpdate when you call publishProgress.
- Type3 is the type of argument you pass to onPostExecute, which is what you return from
doInBackground
.
Answered By - Gordak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.