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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:13:47+00:00 2026-06-13T00:13:47+00:00

I am a beginner in android.I have built an application for login for leave

  • 0

I am a beginner in android.I have built an application for login for leave module.For connecting it with mySql through PHP..I tried this code from a website.

package com.example.axdroid;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Axdroid extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_axdroid);

    b = (Button)findViewById(R.id.Button01); 
    et = (EditText)findViewById(R.id.username);
    pass= (EditText)findViewById(R.id.password);
    tv = (TextView)findViewById(R.id.tv);

    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog = ProgressDialog.show(Axdroid.this, "",
                   "Validating user...", true);
            Intent i=new Intent(getApplicationContext(),Userpage.class);
             startActivity(i);                
             new Thread(new Runnable() {
                    public void run() {
                        login();                         
                    }
                  }).start();         

        }
        });}
void login(){
    try{           

        httpclient=new DefaultHttpClient();
        httppost= new HttpPost("http://192.168.1.222/AndroidLeave/check.php");
        //add your data
        nameValuePairs = new ArrayList<NameValuePair>(2);
        // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
        nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  // $Edittext_value = $_POST['Edittext_value'];
        nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        //Execute HTTP Post Request
        response=httpclient.execute(httppost);
        // edited by James from coderzheaven.. from here....
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        final String response = httpclient.execute(httppost, responseHandler);
        System.out.println("Response : " + response);
        runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("Response from PHP : " + response);
                dialog.dismiss();
            }
        });

        if(response.equalsIgnoreCase("User Found")){
            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(Axdroid.this,"Login Success", Toast.LENGTH_SHORT).show();
                }
            });

            startActivity(new Intent(getApplicationContext(), Userpage.class));
        }else{
            showAlert();               
        }

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
public void showAlert(){
    Axdroid.this.runOnUiThread(new Runnable() {
        public void run() {
            AlertDialog.Builder builder = new AlertDialog.Builder(Axdroid.this);
            builder.setTitle("Login Error.");
            builder.setMessage("User not Found.") 
                   .setCancelable(false)
                   .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                       }
                   });                    
            AlertDialog alert = builder.create();
            alert.show();              
        }
    });
}

}

I am unable to make this work as while debugging the code…the control jumps from httpclient to catch code.Would appreciate any help in sorting this.

This is the PHP file.I have checked it seperately and it is working.

<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);


$username = $_POST['username'];
$password = $_POST['password'];


$query_search = "select * from tbl_user where username = '".$username."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) {
echo "No Such User Found";
}
else  {
echo "User Found";
}
?>

@ashwani
This is the present Axdroid.java page(main activity page)

public class Axdroid extends Activity {
Button b;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_axdroid);

    b = (Button)findViewById(R.id.Button01); 
    et = (EditText)findViewById(R.id.username);
    pass= (EditText)findViewById(R.id.password);
    tv = (TextView)findViewById(R.id.tv);

    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            String url="http://192.168.1.222/AndroidLeave/check.php";
            JSONParser jparser= new JSONParser();
            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("tag", "getcategory"));
            params.add(new BasicNameValuePair("username", et.getText().toString()));
            params.add(new BasicNameValuePair("password", pass.getText().toString()));
            JSONObject jObj= jparser.makeHttpRequest(url, "POST", params);try {
                String success = jObj.get("success").toString();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                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-13T00:13:48+00:00Added an answer on June 13, 2026 at 12:13 am

    for parsing the data I use the following class

    JsonParser.java

    public class JSONParser {
    
        InputStream is = null;
        JSONObject jObj = null;
        String json = "";
    
        public JSONParser() {
        }
    
        // function get json from url
        // by making HTTP POST or GET method
        public JSONObject makeHttpRequest(String url, String method,
                List<NameValuePair> params) {
    
            // Making HTTP request
            try {
    
                // check for request method
                if (method == "POST") {
                    // request method is POST
                    // defaultHttpClient
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
    
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
    
                } else if (method == "GET") {
                    // request method is GET
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                    HttpGet httpGet = new HttpGet(url);
    
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder("");
    
                Log.d("reader value in json parser", reader.toString());
                String line = "";
                while ((line = reader.readLine()) != null) {
                    Log.d("line in JsonParser", line);
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
                Log.d("Json value", json);
    
            } catch (Exception e) {
                Log.d("Buffer Error", "Error converting result " + e.toString());
            }
    
            // try parse the string to a JSON object
            try {
    
                jObj = new JSONObject(json);
                json = "";
            } catch (JSONException e) {
                Log.d("JSON Parser", "Error parsing data " + e.toString());
            }
            return jObj;
        }
    }
    

    now in your activity try to pass a url like this

    String url="place your url here";
    JSONParser jparser= new JSONParser();
    params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", "getcategory"));
    params.add(new BasicNameValuePair("username", username.getText().toString()));
    params.add(new BasicNameValuePair("password", password.getText().toString()));
    JSONObject jobj= jparser.makeHttpRequest(url, "POST", params); 
    

    //now here retrieve the data from json as:

    String success= jobj.get("success");
    

    Hope this helps!

    also in your php file you have to use the json_encode function before echo !

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

Sidebar

Related Questions

I'm a beginner in Android. Here I have this code: public class GPS extends
Beginner Android and Java developer here. I have this snippet of code inside a
Beginner in Android development. My code crashes. I have made a simple Java method
I am a beginner to android and have designed a login GUI for app
I am a beginner in Android. I have developed a application that will send
Android beginner, Can anybody guide me on this ? I do have the distance,
I am implementing a Database for my (beginner/Android) application. It is not clear to
I am a beginner in both Java and Android, and I think this is
I'm a beginner in Android development but not in programming itself. Anyway, this question
Beginner here, I have a simple question. In Android what would be the best

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.