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 9286399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:24:30+00:00 2026-06-18T19:24:30+00:00

I have a login.php script which will validate the username and password entered in

  • 0

I have a login.php script which will validate the username and password entered in the android. The code is below

<?php

    include('dbconnect.php'); 

    $data=file_get_contents('php://input');
    $json = json_decode($data);
    $tablename = "users";

    //username and password sent from android
    $username=$json->{'username'};
    $password=$json->{'password'};

    //protecting mysql injection
    $username = stripslashes($username);
    $password = stripslashes($password);
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
    $password = md5($password);

    $sql = "SELECT id FROM $tablename WHERE u_username='$username' and password='$password'"; 

    //Querying the database
    $result=mysql_query($sql);

    //If found, number of rows must be 1
    if((mysql_num_rows($result))==1){

    //creating session
    session_register("$username");
    session_register("$password");

    print "success";
    }else{
    print "Incorrect details";
    }
?>

I also have an android class from which the user will enter the username and password. The code is below.

  public class LoginActivity extends Activity {


   public static final String loginURI="http://.../login.php";

   @Override
   public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);



    buttonSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            String userID = "";
            userID=login(editTextUsername.getText().toString(), editTextPassword.getText().toString());

            if (editTextPassword.getText().toString() != null & editTextUsername.getText().toString() != null){
                //Used to move to the Cases Activity
                Intent casesActivity = new Intent(getApplicationContext(), CasesActivity.class);
                startActivity(casesActivity);
                casesActivity.putExtra("username", userID);

            }
            else{
                //Display Toaster for error
                Toast.makeText(getApplicationContext(),"this is an error message", Toast.LENGTH_LONG).show();
            }
        }
    });


private String login(String username, String password){

    JSONObject jsonObject = new JSONObject();

    String success = "";

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(loginURI);
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams,10000);
    HttpConnectionParams.setSoTimeout(httpParams,10000);

   try {

        jsonObject.put("username", username);
        Log.i("username", jsonObject.toString());
        jsonObject.put("password", password);
        Log.i("password", jsonObject.toString());

        StringEntity stringEntity = new StringEntity(jsonObject.toString());
        stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpPost.setEntity(stringEntity);
        HttpResponse httpResponse = httpClient.execute(httpPost);

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {
            success = EntityUtils.toString(httpResponse.getEntity());
            Log.i("success", success);
        }

    }catch (IOException e){
        Log.e("Login_Issue", e.toString());
    }catch (JSONException e) {
        e.printStackTrace();  
    }


    return success;
  }

}

I get the following error: ERROR/AndroidRuntime(29611): FATAL EXCEPTION: main android.os.NetworkOnMainThreadException.
I understand that I need another thread and I was thinking of using AsyncTask, but I do not know where to put it in this class.

Could you also give me some advice in using JSON for sending and receiving data from android.

Thank you for your help,

  • 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-18T19:24:31+00:00Added an answer on June 18, 2026 at 7:24 pm

    you can change your code using AsyncTask by calling login method inside doInBackground and start next Activity on onPostExecute when login successful as :

    private class LoginOperation extends AsyncTask<String, Void, String> {
    
    String str_username=;
    String str_password=;
    
         public LoginOperation(String str_username,String str_password){
          this.str_password=  str_password;
          this.str_username=  str_username;
         }
         @Override
          protected void onPreExecute() {
    
           // show progress bar here
          }
          @Override
          protected String doInBackground(String... params) {
               // call login method here
             String userID=login(str_username,str_password);
             return userID;
          }      
    
          @Override
          protected void onPostExecute(String result) {    
             // start next Activity here
               if(result !=null){
                    Intent casesActivity = new Intent(getApplicationContext(),
                                                    CasesActivity.class);
                    casesActivity.putExtra("username", result);
                    Your_Activiy.this.startActivity(casesActivity);
    
                   }
          }
    

    and start LoginOperation AsyncTask on button click as:

    buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                if (editTextPassword.getText().toString() != null 
                             & editTextUsername.getText().toString() != null){
                    // start AsyncTask here
                     new LoginOperation(editTextUsername.getText().toString(),
                            editTextPassword.getText().toString()).execute("");
                }
                else{
                  // your code here
                }
            }
        });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file which checks for login and password from users database,
I have a script below which the user logs a php form. There is
I am using my admin panel login script where i have created a global.php
I have a PHP login which sets 2 cookies once someone login. The problem
I have a pagination script which I have posted below, the problem is I
We have some folders on our website which are password protected through a PHP
I have a login script which generally works for me, but occasionally goes into
So I have a script which checks the login details.. I have that working,
I have php scripts that do things like register, login, upload. I would like
Assume that I have login to system using PHP + MySQL, then, after I

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.