我只处理PHP,MySql和json部分。
如果用户在android应用中输入用户名和密码,则需要使用PHP和Mysql检查用户表,并且需要使用json仅发送状态代码(例如1或0)。
如果用户名和密码匹配,则状态= 1需要发送到android应用编码,否则为0。
状态检查过程需要在Android编码部分中完成。
如果status = 1,并且我需要重定向到android应用中的另一个窗口。
我看到了很多问题,但没有任何帮助。
因此,请帮助我如何将状态从PHP发送到android应用,以及如何获取该状态并在android中进行验证。
#1 楼
我已经为我的项目完成了这段代码。而且效果很好。试试看。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
?>
Android的Java代码
login.java
/>使用两个edittext和按钮,然后在按钮上单击以放置此代码。
删除您不希望将用户名,emp_id保存到整个应用程序的saveperference方法。
@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();
}
}
}
}
}
#2 楼
您的android客户端必须在您的PHP脚本中进行Http发布,以返回状态代码。您的脚本会将该请求视为其他任何发布请求。客户端从服务器接收结果,并进行所需的计算。#3 楼
只需遵循此代码,我认为它的工作就很好...$email = isset($_REQUEST['email'])?urldecode($_REQUEST['email']):"";
$passwd= isset($_REQUEST['passwd'])?urldecode($_REQUEST['passwd']):"";
if($email==""||$passwd=="" )
{
$return_data['status'] = 0;
}
else
{
$user_info="select id from table name WHERE email=$emailAND passwd=$passwd";
if($user_info)
{
$return_data['status'] = 1;
}
else
{
$return_data['status'] = 0;
}
}
echo json_encode($return_data);
评论
嗨,我已经发送了类似echo echo json_encode($ return_data);的信息,在此之后,如何将此状态值获取到Android App中。
–user1802184
2012年11月6日在7:14
您只需要调用网络服务,传递电子邮件和passwd的值...之后,您就会收到响应...
– heart_hacker
2012年11月6日7:19
isset($ _ REQUEST ['email'])?urldecode($ _ REQUEST ['email']):“”; ,您能否说出此功能的确切作用?
–user1802184
2012年11月6日7:24
如果您传递的电子邮件值包含任何特殊字符,请使用urldecode避免该字符.....只需在webservices的帮助下传递您的电子邮件和密码http post方法。
– heart_hacker
2012年11月6日7:48
评论
现在就试试。让我知道是否还有其他错误。
– Brohi先生
2012年11月12日上午10:15
这对我帮助很大。但是,在这段代码中,您没有正确使用json概念。你能用json ..做吗?
–user1802184
2012年11月16日下午4:32