How to validate the user login credentials in android using PHP, MySql with the help of json

后端 未结 3 1823
暖寄归人
暖寄归人 2021-01-06 06:01

I am new to android development. I want to do the login verification, using php mysql and json.

I am only taking care of the PHP, MySql and json part.

If t

3条回答
  •  清歌不尽
    2021-01-06 06:34

    I have done this code for my project. and it works fine. Give it a try.

    Login2.php

     0)
    echo mysql_result($result,0);  // for correct login response
    else
    echo 0; // for incorrect login response
    ?>
    

    Java Code for Android login.java

    take two edittext and button and on button click put this code. delete saveperference method it you dont want to save username , emp_id to entire application.

        @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
    
                        ArrayList postParameters = new ArrayList();
                        postParameters.add(new BasicNameValuePair("username", username
                                .getText().toString()));
                        postParameters.add(new BasicNameValuePair("password", password
                                .getText().toString()));
                        // String valid = "1";
                        String response = null;
                        try {
                            // "http://10.0.2.2//Mobile/login2.php"
                            response = CustomHttpClient
                                    .executeHttpPost(
                                            "http://10.0.2.2//Mobile/login2.php",
                                            postParameters);
    // now in result you will have the response from php file either 0 or 1.                        
    result = response.toString();
                            // res = res.trim();
                            result = result.replaceAll("\\s+", "");
                            // error.setText(res);
    
                            if (!result.equals("0")) {
                                SavePreferences("name", "pass", "emp_id", username
                                        .getText().toString(), password.getText()
                                        .toString());
                                Intent in = new Intent(Login.this, MainScreen.class);
    
                                // LoadPreferences();
                                error.setText("");
    
                                startActivity(in);
                            }
    
                            else
                                error.setText("Incorrect Username or Password");
    
                        } catch (Exception e) {
                            // un.setText(e.toString());
                        }
    
                    }
    

    CustumHttpClient.java

    public class CustomHttpClient {
        /** The time it takes for our client to timeout */
        public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
    
        /** Single instance of our HttpClient */
        private static HttpClient mHttpClient;
    
        /**
         * Get our single instance of our HttpClient object.
         *
         * @return an HttpClient object with connection parameters set
         */
        private static HttpClient getHttpClient() {
            if (mHttpClient == null) {
                mHttpClient = new DefaultHttpClient();
                final HttpParams params = mHttpClient.getParams();
                HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
                HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
                ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
            }
            return mHttpClient;
        }
    
        /**
         * Performs an HTTP Post request to the specified url with the
         * specified parameters.
         *
         * @param url The web address to post the request to
         * @param postParameters The parameters to send via the request
         * @return The result of the request
         * @throws Exception
         */
        public static String executeHttpPost(String url, ArrayList postParameters) throws Exception {
            BufferedReader in = null;
            try {
                HttpClient client = getHttpClient();
                HttpPost request = new HttpPost(url);
                UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
                request.setEntity(formEntity);
                HttpResponse response = client.execute(request);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
    
                String result = sb.toString();
                return result;
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        /**
         * Performs an HTTP GET request to the specified url.
         *
         * @param url The web address to post the request to
         * @return The result of the request
         * @throws Exception
         */
        public static String executeHttpGet(String url) throws Exception {
            BufferedReader in = null;
            try {
                HttpClient client = getHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(url));
                HttpResponse response = client.execute(request);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    
                StringBuffer sb = new StringBuffer("");
                String line = "";
                String NL = System.getProperty("line.separator");
                while ((line = in.readLine()) != null) {
                    sb.append(line + NL);
                }
                in.close();
    
                String result = sb.toString();
                return result;
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

提交回复
热议问题