the following code run just fine in android 2.3 but when i change teh target sdk to 15 it gives an error and shutdown the application once i press login button..
i've searched a lot and figure out that is because am trying to access an external database from the main method or something like this and i have to do this kind of process in another thread using AsynkTask but i don't know how ??
this is my login code
public class LoginActivity extends Activity {
Button btnLogin;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;
// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Importing all assets like buttons, text fields
inputEmail = (EditText) findViewById(R.id.loginEmail);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
UserFunctions userFunction = new UserFunctions();
Log.d("Button", "Login");
JSONObject json = userFunction.loginUser(email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
loginErrorMsg.setText("");
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(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.getString(KEY_UID));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username or password");
loginErrorMsg.setVisibility(1);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
Most probably you are getting a NetworkOnMainThreadException
.
This exception is thrown when an application attempts to perform a networking operation on its main thread.
It was added in API level 11, so yes, in 2.3 there won't be problems, but starting with versions 3.0 and higher the exception will be thrown.
The solution would be to put the part of code that performs network operations in an AsyncTask
, in your case the login, and retrieval of JSON data.
The most used methods of an AsyncTask
are:
onPreExecute(), invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.
doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time.
onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
Here are some resource to get you started with AsyncTask:
http://developer.android.com/reference/android/os/AsyncTask.html
http://androidresearch.wordpress.com/2012/03/17/understanding-asynctask-once-and-forever/