使用php mysql的Android登录屏幕
我已成功创建登录页面。。。我已经成功地在数据库中检查了用户名和密码。然后我设置了条件使用php mysql的Android登录屏幕,php,android,Php,Android,我已成功创建登录页面。。。我已经成功地在数据库中检查了用户名和密码。然后我设置了条件如果(result!=null)意味着开始第二个活动。 我现在得到的是什么呢?不,即使用户名或密码也不正确,它会启动第二页。我要做什么才能使正确的登录屏幕。。。 Suggestion plz,提前谢谢 AsynTask onpostexecute函数 protected void onPostExecute(String result) { //View your result here.
如果(result!=null)
意味着开始第二个活动。
我现在得到的是什么呢?不,即使用户名或密码也不正确,它会启动第二页。我要做什么才能使正确的登录屏幕。。。
Suggestion plz,提前谢谢
AsynTask onpostexecute函数
protected void onPostExecute(String result) {
//View your result here.
if(result!= null)
{
val=result;
Intent intent=new Intent(Login.this,Child_Profile.class);
intent.putExtra("child_name", result);
startActivity(intent);
}
}
我的login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="6"
android:background="#1E8EB6" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#1E8EB6"
android:layout_weight="1.5">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:gravity="center_vertical"
android:text="@string/title"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="94dp"
android:layout_marginTop="21dp"
android:contentDescription="@string/title_img"
android:src="@drawable/camera" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#00677F"
android:layout_weight="3.5"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="10dp"
android:paddingLeft="5dp"
android:ems="10"
android:background="#FFFFFF"
android:drawableLeft="@android:drawable/stat_notify_more"
android:inputType="text"
android:gravity="center_vertical"
android:hint="@string/username">
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@+id/username"
android:layout_centerHorizontal="true"
android:ems="10"
android:background="#FFFFFF"
android:drawableStart="@android:drawable/ic_secure"
android:inputType="textPassword"
android:paddingLeft="5dp"
android:gravity="center_vertical"
android:hint="@string/password"/>
<Button
android:id="@+id/login"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignRight="@+id/password"
android:layout_below="@+id/password"
android:layout_marginTop="19dp"
android:text="@string/login"
android:background="#054579"
android:textColor="#FFFFFF" />
<CheckBox
android:id="@+id/remember"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/login"
android:layout_alignBottom="@+id/login"
android:layout_alignLeft="@+id/password"
android:text="@string/remember"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/forget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="@string/forget"
android:textColor="#FFFFFF"
style="?android:attr/borderlessButtonStyle"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<Button
android:id="@+id/signup"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#054579"
android:text="@string/signup"
android:textColor="#FFFFFF" />
</RelativeLayout>
</LinearLayout>
My Login.java,最终工作代码
package com.example.certify.schoolapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
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 Login extends Activity {
private TextView head;
private EditText username,password;
private Button login,signup,forget;
private static String user,pass,val,result="";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
head=(TextView) findViewById(R.id.textView1);
username=(EditText) findViewById(R.id.username);
password=(EditText) findViewById(R.id.password);
login=(Button) findViewById(R.id.login);
signup=(Button) findViewById(R.id.signup);
forget=(Button) findViewById(R.id.forget);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new sendPostData().execute();
}
});
}
private class sendPostData extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String... params) {
try{
user = username.getText().toString();
pass = password.getText().toString();
String link="http://192.168.1.5:81/arun/loginpost.php?";
String data = URLEncoder.encode("username", "UTF-8")
+ "=" + URLEncoder.encode(user, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8")
+ "=" + URLEncoder.encode(pass, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter
(conn.getOutputStream());
wr.write( data );
wr.flush();
BufferedReader reader = new BufferedReader
(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
return sb.toString();
}catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
//View your result here.
if(result.equals("wrong"))
{
Toast toast=Toast.makeText(Login.this,"incorrect password", Toast.LENGTH_LONG);
toast.show();
}
else
{
Intent intent=new Intent(Login.this,Child_Profile.class);
intent.putExtra("child_name", result);
startActivity(intent);
}
}
}
}
package com.example.certificate.schoolapp;
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.io.OutputStreamWriter;
导入java.net.URL;
导入java.net.URLConnection;
导入java.net.urlcoder;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类登录扩展活动{
私有文本视图头;
私有文本用户名、密码;
私人按钮登录,注册,忘记;
私有静态字符串user,pass,val,result=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
head=(TextView)findViewById(R.id.textView1);
用户名=(EditText)findViewById(R.id.username);
密码=(EditText)findViewById(R.id.password);
login=(按钮)findviewbyd(R.id.login);
注册=(按钮)findViewById(R.id.signup);
忘记=(按钮)findViewById(R.id.forget);
login.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
新建sendPostData().execute();
}
});
}
私有类sendPostData扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
user=username.getText().toString();
pass=password.getText().toString();
字符串链接=”http://192.168.1.5:81/arun/loginpost.php?";
字符串数据=URLEncoder.encode(“用户名”,“UTF-8”)
+“=”+URLEncoder.encode(用户,“UTF-8”);
数据+=“&”+URLCoder.encode(“密码”,“UTF-8”)
+“=”+URLEncoder.encode(通过“UTF-8”);
URL=新的URL(链接);
URLConnection conn=url.openConnection();
连接设置输出(真);
OutputStreamWriter wr=新的OutputStreamWriter
(conn.getOutputStream());
wr.写入(数据);
wr.flush();
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(conn.getInputStream());
StringBuilder sb=新的StringBuilder();
字符串行=null;
//读取服务器响应
而((line=reader.readLine())!=null)
{
某人附加(行);
打破
}
使某人返回字符串();
}捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//在这里查看您的结果。
如果(结果等于(“错误”))
{
Toast Toast=Toast.makeText(Login.this,“密码不正确”,Toast.LENGTH\u LONG);
toast.show();
}
其他的
{
意向意向=新意向(Login.this,Child_Profile.class);
意图。putExtra(“孩子的名字”,结果);
星触觉(意向);
}
}
}
}
我的loginpost.php
<?php
$con=mysql_connect("localhost","arun","sachin11");
$db_select = mysql_select_db('my_db', $con);
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("SELECT Role FROM table1 where Username='$username' and Password='$password'",$con);
$row = mysql_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
else
{
echo "wrong";
}
mysql_close($con);
?>
无论从doInBackground
传递什么,因为字符串值是onPostExecute
中的结果
查看您的doInBackground
似乎没有一条路径,返回的字符串永远不能为null
此外,检查是否将其设置为用户成功身份验证的指示也是不好的。相反,我会传回一个布尔值或更好的用户对象,这是您在中使用用户详细信息创建的
我会改变:
if(result!= null)
到
其中OK
是成功登录时服务器的预期响应。
[不推荐,我怀疑你的PHP脚本会返回这个结果。]if(result.equals(“user1”)){},它可以工作,,,但是我动态地获取user1,,,如何现在进行mysql查询,我想你现在会理解$result=mysql\u查询(“从表1中选择角色,其中Username='$Username'和Password='$Password',$con”);是要动态填充“user1字符串”还是要读取“角色”“在结果中也是如此?如果用户名和密码正确表示,,,我将获取角色值,,,然后我要检查条件,,,如果为true表示开始活动,,,否则显示toast或执行其他操作以实现此目的。从技术上讲,如果执行SQL并且它不返回任何行,则应该从服务器调用中返回空字符串。”。因此,您可以将逻辑更改为if(!results.equals(“”)。这似乎与然后被传递到您的新活动一起工作。但是,我会将catch块更改为返回“”;也不例外。
if(result.eques("OK"))