views:

94

answers:

2

Hello, I am trying to do what I think is a fairly simple task to authenticate a user on my server. I am using AsyncTask as a private subclass of my Activity, however when I try to populate the user object after authenticating it keeps setting it to null. Is there something strange in how the onPostExecute() method is called that is causing this? I originally had the AsyncTask as its own class but ran into the same problem, so I am try to solve this using a private subclass. Any help? Thanks.

Here is my code:

public class Testing extends Activity {

    private User user;

    public User getUser() {
        return this.user;
    }

    public void setUser(User value) {
        this.user = value;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView iconImage = (ImageView) this.findViewById(R.id.image);
        final Button testButton = (Button) this.findViewById(R.id.testButton);
        final TextView text = (TextView) this.findViewById(R.id.text);
        iconImage.setImageResource(R.drawable.logo_real);
        testButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                LoginTask loginTask = (LoginTask) new LoginTask().execute(new Void[0]);
                text.setText(user.getFormattedFullName());
            }
        });
    }

    private class LoginTask extends AsyncTask<Void, Void, User> {

        public LoginTask() {
        }

        protected User doInBackground(Void... params) {
            User newUser = new User();
            try {
                System.out.println("testing task");
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet("http://10.0.2.2:8080/Dugout/[email protected]&amp;PASSWORD=password");
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                String message = "";
                String inputLine = "";
                BufferedReader bf = new BufferedReader(new InputStreamReader(is));
                while ((inputLine = bf.readLine()) != null) message+=inputLine;
                JSONObject returnedUser = new JSONObject(message);
                int userId = returnedUser.getInt("userId");
                boolean loginSuccess = returnedUser.getBoolean("login_success");
                String firstName = returnedUser.getString("firstName");
                String lastName = returnedUser.getString("lastName");
                newUser.setId(userId);
                newUser.setFirstName(firstName);
                newUser.setLastName(lastName);
                newUser.setLoginSuccess(loginSuccess);
                return newUser;
            } catch (Exception e) {
                newUser.setLoginSuccess(false);
                return newUser;
            }
        }

        protected void onPostExecute(User user) {
            setUser(user);
        }

    }

}
+1  A: 

Should be

protected void onPostExecute(User user) {
    text.setText(user.getFormattedFullName());
}

You are trying to access user before it's been created by AsyncTask (the whole point is that doInBackground(..) runs in a background thread therefore not blocking the UI thread).

alex
A: 

That makes sense. I appreciate the answer. I guess that begs my next question, which is should I be using a Service instead of an AsyncTask here? The goal is to login the user and return a user object, so that I can move onto the next Activity and show the options for that user. Seems as though I'd want the UI thread to hold until that is completed (I think I answered my own question but validation from the experts is good).

Thanks.

Chris
Services are not meant to be used in that way, AsyncTask is a good choice, just follow what alex said, and change your doInBackgroundCode. If you want to stop the user from messing with UI while getting user info, use progress dialog.
Rabas