我是android开发的新手.我想使用PHP MysqL和Json进行登录验证.
我只关心PHP,MysqL和Json部分.
如果用户在Android应用程序中输入用户名和密码,则需要使用PHP和MysqL检查用户表,并且需要使用Json仅发送状态代码,如1或0.
如果用户名和密码匹配,那么status = 1需要发送到androID app编码,否则为0.
状态检查过程需要在AndroID编码部分完成.
如果status = 1,我需要重定向到AndroID应用程序中的另一个窗口.
我见过很多东西,但没有任何帮助我.
所以,请帮助我如何将状态从PHP发送到AndroID应用程序以及如何获取该状态并在androID中验证.
解决方法:
我为我的项目完成了这个代码.它工作正常.试试看.
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 happensif(MysqL_num_rows($result) > 0)echo MysqL_result($result,0); // for correct login responseelseecho 0; // for incorrect login response?>
适用于AndroID的Java代码
login.java
取两个edittext和按钮,然后单击按钮放置此代码.
删除saveperference方法你不想保存用户名,emp_ID到整个应用程序.
@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
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(); } } } }}
总结 以上是内存溢出为你收集整理的如何在json的帮助下使用PHP,MySql验证android中的用户登录凭据全部内容,希望文章能够帮你解决如何在json的帮助下使用PHP,MySql验证android中的用户登录凭据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)