如何验证使用PHP,MySQL的android系统中的用户登录凭证使用JSON的帮助 [英] How to validate the user login credentials in android using PHP, MySql with the help of json

查看:521
本文介绍了如何验证使用PHP,MySQL的android系统中的用户登录凭证使用JSON的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android开发。我想做的登录验证,使用PHP MySQL和JSON。

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

我只服用了PHP,MySQL和JSON一部分的照顾。

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

如果用户输入的Andr​​oid应用程序的用户名和密码,然后需要检查使用PHP和MySQL用户表,它需要只发送状态code像1或0使用JSON。

If the user enters the username and password in android app, then it needs to check the user table using PHP and Mysql and it needs to send only the status code like 1 or 0 using json.

如果用户名和密码相匹配,则状态= 1需要发送到Android应用程序编码,否则为0。

If username and password matches then the status = 1 needs to send to the android app coding, else 0.

状态检查过程需要在Android的编码部分进行。

Status checking process need to be done in Android Coding part.

如果状态= 1,我需要重定向到Android应用程序的另一个窗口。

If status = 1 and I need to redirect to another window in android app .

我已经看到了很多TUTS和,但没有帮助我。

I have seen lots of tuts and but nothing has helped me.

那么,请您帮助我如何的状态 PHP发送到Android应用程序,以及如何为获取状态和验证中的Andr​​oid

So, please kindly help me how to send the status from PHP to android app and how to get that status and validate in android.

推荐答案

我已经为我的项目做到了这一点code。并能正常工作。试试看吧。

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

Login2.php

Login2.php

<?php
$un=$_POST['username'];
$pw=$_POST['password'];
//connect to the db

$host="localhost"; // Host name 
$user="root"; // Mysql username 
$pswd=""; // Mysql password 
$db="database_name"; // Database name 
$tbl_name="user_info"; // Table name

$conn = mysql_connect($host, $user, $pswd);
mysql_select_db($db, $conn);
//run the query to search for the username and password the match
//$query = "SELECT * FROM $tbl_name WHERE first_name = '$un' AND password = '$pw'";
$query = "SELECT emp_id FROM $tbl_name WHERE first_name = '$un' AND password = '$pw'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
//this is where the actual verification happens
if(mysql_num_rows($result) > 0)
echo mysql_result($result,0);  // for correct login response
else
echo 0; // for incorrect login response
?>

Java的code为Android login.java

Java Code for Android login.java

取两个的EditText和按钮上按一下按钮把这个code。 删除saveperference方法是你不想要保存的用户名,emp_id为到整个应用程序。

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<NameValuePair> postParameters = new ArrayList<NameValuePair>();
                    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

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

这篇关于如何验证使用PHP,MySQL的android系统中的用户登录凭证使用JSON的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆