我已成功创建了登录页面...我已成功检查了我的数据库的用户名和密码。然后我设置了条件if(result!=null)
表示启动第二个活动。
我现在得到的是什么,即使用户名或密码也不正确,它也会启动第二页。我需要做些什么来制作正确的登录界面...
Suggesstion 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>
我的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);
}
}
}
}
我的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);
?>
答案 0 :(得分:0)
无论您从doInBackground
传出什么,因为字符串值是Result
中的onPostExecute
。
查看您的doInBackground
似乎没有返回字符串永远不会是null
的路径。
此外,检查是否设置或不设置为用户成功验证的指示是不好的。我会使用用户详细信息传回一个布尔或更好的用户对象。
我会改变:
if(result!= null)
到
if(result.eques("OK"))
其中OK
是成功登录时服务器的预期响应。
[不是真的推荐,我怀疑你的PHP脚本会返回它。]