Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:53:45+00:00 2026-06-16T19:53:45+00:00

the following code run just fine in android 2.3 but when i change teh

  • 0

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();
            }
        }
    });


}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:53:46+00:00Added an answer on June 16, 2026 at 7:53 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code gives a compiler error (gcc-4.7 run with -std=c++11 ): #include <iostream>
I have following code written to run NSTimer . But the NSTimer selector is
I've created the following in my 2.3.3 application (This is just a code snippet).
I have the following code being run if an iphone/ipad/ipod is detected: $('.tooltip-trigger').tooltip({offset: [20,
The following code does not run as rootNode is null when retrieved by name
The following code fails to run on Python 2.5.4: from matplotlib import pylab as
The following code words if run in the console itself: var $inputbox = $('input#inputfield');
when I trying to run following code. var result = from c in db.brand
Let's say that I have the following code that's run in one thread of
What is the check_union256d function? It's placed in following code: /* { dg-do run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.