title: Android 4.0 Login and Registration using JSON
body: <p>I'm trying to do login and regitration on an Android 4.0 device.  So I had to update my code to use Asynctask and I'm getting errors and not sure how to fix them.  Any help would be greatly appreciated!</p>
  
  <pre><code>public class RegisterActivity extends Activity {
  Button btnRegister;
  Button btnLinkToLogin;
  EditText inputFullName;
  EditText inputEmail;
  EditText inputPassword;
  TextView registerErrorMsg;
  
  //JSON Response node names
  private static String KEY_SUCCESS = "success";
  private static String KEY_UID = "uid";
  private static String KEY_NAME = "name";
  private static String KEY_EMAIL = "email";
  private static String KEY_CREATED_AT = "created_at";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_register);
  
      // Importing all assets like buttons, text fields
      inputFullName = (EditText) findViewById(R.id.registerName);
      inputEmail = (EditText) findViewById(R.id.registerEmail);
      inputPassword = (EditText) findViewById(R.id.registerPassword);
      btnRegister = (Button) findViewById(R.id.btnRegister);
      btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
      registerErrorMsg = (TextView) findViewById(R.id.register_error);
  
      // Register Button Click event
      btnRegister.setOnClickListener(new View.OnClickListener() {
          public void onClick(View view) {
  
              new RegisterTask().execute();
          }
      });
  
      // Link to Login Screen
      btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View view) {
              Intent i = new Intent(getApplicationContext(),
                      LoginActivity.class);
              startActivity(i);
              // Close Registration View
              finish();
          }
      });
  }
  
  class RegisterTask extends AsyncTask&lt;String, String, String&gt; {
  int success;
  String email;
  String password;
  String name;
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
      email = inputEmail.getText().toString();
      password = inputPassword.getText().toString();
      name = inputFullName.getText().toString();
  }
  
  @Override
  protected String doInBackground(String... args) {
      UserFunctions userFunction = new UserFunctions();
      JSONObject json = userFunction.registerUser(name, email, password);
  
      Log.d("Create Response", json.toString());
  
      try {
          int success = json.getInt(KEY_SUCCESS);
          if (success == 1) {
              // DATABASE HANDLER
              // user successfully logged in
              // Store user details in SQLite Database
              DatabaseHandler db = new DatabaseHandler(
                      getApplicationContext());
              JSONObject json_user = json.getJSONObject("user");
  
              // Clear all previous data in database
              userFunction.logoutUser(getApplicationContext());
  
              db.addUser(json_user.getString(KEY_NAME),
                      json_user.getString(KEY_EMAIL),
                      json_user.getString(KEY_UID),
                      json_user.getString(KEY_CREATED_AT));
  
          }
      } catch (JSONException e) {
          e.printStackTrace();
      }
      return null;
  }
  
  protected void onPostExecute(String file_url) {
  if (success == 1) {
          Intent dashboard = new Intent(getApplicationContext(), LoginActivity.class);
          dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(dashboard);
          finish();
  }else{
      //tvLError.setText("Incorrect username/password");
  
   }
  }
  }
  }
  </code></pre>
  
  <p>And the error log I am getting...</p>
  
  <p>12-02 20:45:23.140: E/JSON Parser(28208): Error parsing data org.json.JSONException: Value </p>
  
  <pre><code>&lt;br of type java.lang.String cannot be converted to JSONObject
  12-02 20:45:23.140: W/dalvikvm(28208): threadid=13: thread exiting with uncaught exception (group=0x415e62a0)
  12-02 20:45:23.140: E/AndroidRuntime(28208): FATAL EXCEPTION: AsyncTask #1
  12-02 20:45:23.140: E/AndroidRuntime(28208): java.lang.RuntimeException: An error occured while executing doInBackground()
  12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$3.done(AsyncTask.java:299)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.lang.Thread.run(Thread.java:856)
  12-02 20:45:23.140: E/AndroidRuntime(28208): Caused by: java.lang.NullPointerException
  12-02 20:45:23.140: E/AndroidRuntime(28208): at com.example.cs309.gotoclass.RegisterActivity$RegisterTask.doInBackground(RegisterActivity.java:89)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at com.example.cs309.gotoclass.RegisterActivity$RegisterTask.doInBackground(RegisterActivity.java:1)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at android.os.AsyncTask$2.call(AsyncTask.java:287)
  12-02 20:45:23.140: E/AndroidRuntime(28208): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  12-02 20:45:23.140: E/AndroidRuntime(28208): ... 5 more
  </code></pre>
  
  <p>registerUser</p>
  
  <pre><code>public JSONObject registerUser(String name, String email, String password){
      // Building Parameters
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("tag", register_tag));
      params.add(new BasicNameValuePair("name", name));
      params.add(new BasicNameValuePair("email", email));
      params.add(new BasicNameValuePair("password", password));
  
      // getting JSON Object
      JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
      // return json
      return json;
  }
  </code></pre>