What is the purpose of the return statement in ASynctask doInBackground ()?

This aspect of my login system works fine if I have a return statement set to 0 or 1, but fails if I use null. This is all adapted from http://256design.com/blog/android-login-asynctask/ where this particular return looks like below my own code.

public LoginTask(Polling activity, ProgressDialog progressDialog)
        this.activity = activity;
        this.progressDialog = progressDialog;

protected Integer doInBackground(String... arg0) {
            EditText userName = (EditText)activity.findViewById(R.id.emailEditText);
            EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText);

            String email = userName.getText().toString();
            String password = passwordEdit.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.loginUser(email, password);
            // check for login response
            //Log.v("test", Integer.toString(jsonParser.getResponseCode()));
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    String res = json.getString(KEY_SUCCESS);

                    if(Integer.parseInt(res) == 1){
                        //user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");
                        //Log.v("name", json_user.getString(KEY_NAME));
                        // Clear all previous data in database
                        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), 
                                json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                        // Close Login Screen
                        //loginErrorMsg = (TextView)activity.findViewById(R.id.loginErrorMsg);
                        //loginErrorMsg.setText("logged in");
                        // Error in login
                        //progressDialog.setMessage("Incorrect username or password");
                        //loginErrorMsg.setText("Incorrect username/password");


            } catch (NullPointerException e) {

            catch (JSONException e) {

            return 1;


The tutorial I used, take a look at the responseCode:

protected Integer doInBackground(String... arg0) 
    String result = "";
    int responseCode = 0;
        HttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.256design.com/projectTransparency/project/headerLogin.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("emailAddress", arg0[0]));
            nameValuePairs.add(new BasicNameValuePair("password", arg0[1]));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        int executeCount = 0;
        HttpResponse response;
            progressDialog.setMessage("Logging in.. ("+(executeCount+1)+"/5)");
            // Execute HTTP Post Request
            response = client.execute(httppost);
            responseCode = response.getStatusLine().getStatusCode();                        
            // If you want to see the response code, you can Log it
            // out here by calling:
            // Log.d("256 Design", "statusCode: " + responseCode)
        } while (executeCount < 5 && responseCode == 408);

        BufferedReader rd = new BufferedReader(new InputStreamReader(

        String line;
        while ((line = rd.readLine()) != null)
            result = line.trim();
        id = Integer.parseInt(result);
    catch (Exception e) {
        responseCode = 408;
    return responseCode;



source to share

4 answers

The goal is to pass the result of your work (running on a worker thread) to onPostExecute

in order to process the result on the UI thread. This is required if you want to update the user interface in response to a successful job launch.



The return value of DoInBackground for the postExecute method and passing null does not validate the condition:

if(headerCode == 202)




Yes, this is the value you are sending to postExecute


http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute(Result )

protected void onPostExecute (result result)

Runs on the UI thread after doInBackground (Params ...).

The specified result is the return value of by doInBackground (Params ...).

This method will not be called if the task has been canceled.

Parameters: result The result of the operation, calculated by doInBackground (Params ...).



protected class InitTask extends AsyncTask<Context, Integer, Integer>


In the above line, we define our subclass and three parameters that will be passed to the callbacks. The callbacks look like this:

doInBackground ()

protected Integer doInBackground( Context... params )  {
     return super.doInBackground( params )


Everything processed in this method is processed in the sperate branch. Note that the data type of the return value is Integer and corresponds to the third type parameter in the class definition. The value returned by this method is passed to the onPostExecute () method when this thread ends .

And if you pass null in reverse order , then this will fail when using the methodonPostExecuted()



All Articles