使用php和mysql登录android中的活动开发,但什么都没有发生
我的使用php和mysql登录android中的活动开发,但什么都没有发生,android,onclick,Android,Onclick,我的admin\u登录Java类: public class admin_login extends AppCompatActivity { public static final int CONNECTION_TIMEOUT=10000; public static final int READ_TIMEOUT=15000; private EditText etEmail; private EditText etPassword; @Overrid
admin\u登录
Java类:
public class admin_login extends AppCompatActivity {
public static final int CONNECTION_TIMEOUT=10000;
public static final int READ_TIMEOUT=15000;
private EditText etEmail;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_login_main_lay);
// Get Reference to variables
etEmail = (EditText) findViewById(R.id.textView2);
etPassword = (EditText) findViewById(R.id.textView4);
}
// Triggers when LOGIN Button clicked
public void checkLogin(View arg0) {
// Get text from email and passord field
final String email = etEmail.getText().toString();
final String password = etPassword.getText().toString();
Toast.makeText(admin_login.this, "User Name or Password does not match", Toast.LENGTH_LONG).show();
// Initialize AsyncLogin() class with email and password
new AsyncLogin().execute(email,password);
}
private class AsyncLogin extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(admin_login.this);
HttpURLConnection conn;
URL url = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}
@Override
protected String doInBackground(String... params) {
try {
// Enter URL address where your php file resides
url = new URL("http://www.uprtou.ac.in/and_login.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "exception";
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
try {
conn = (HttpURLConnection)url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput method depict handling of both send and receive
conn.setDoInput(true);
conn.setDoOutput(true);
// Append parameters to URL
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("username", params[0])
.appendQueryParameter("password", params[1]);
String query = builder.build().getEncodedQuery();
// Open connection for sending data
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return "exception";
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return(result.toString());
} else {
return("unsuccessful");
}
} catch (IOException e) {
e.printStackTrace();
return "exception";
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
//this method will be running on UI thread
pdLoading.dismiss();
if(result.equalsIgnoreCase("true")) {
/* Here launching another activity when login successful. If you persist login state
use sharedPreferences of Android. and logout button to clear sharedPreferences.
*/
Intent intent = new Intent(admin_login.this,admin_home.class);
startActivity(intent);
admin_login.this.finish();
} else if (result.equalsIgnoreCase("false")) {
// If username and password does not match display a error message
Toast.makeText(admin_login.this, "Invalid email or password", Toast.LENGTH_LONG).show();
} else if (result.equalsIgnoreCase("exception") || result.equalsIgnoreCase("unsuccessful")) {
Toast.makeText(admin_login.this, "OOPs! Something went wrong. Connection Problem.", Toast.LENGTH_LONG).show();
}
}
}
}
public class admin\u登录扩展了AppCompative活动{
公共静态最终int连接\u超时=10000;
公共静态最终整型读取超时=15000;
私人编辑邮件;
私人编辑文本和密码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.admin\u login\u main\u lay);
//获取对变量的引用
etEmail=(EditText)findViewById(R.id.textView2);
etPassword=(EditText)findViewById(R.id.textView4);
}
//单击登录按钮时触发
公共无效检查登录(视图arg0){
//从电子邮件和密码字段获取文本
最终字符串email=etEmail.getText().toString();
最终字符串password=etPassword.getText().toString();
Toast.makeText(admin_login.this,“用户名或密码不匹配”,Toast.LENGTH_LONG.show();
//使用电子邮件和密码初始化AsyncLogin()类
新建AsyncLogin().execute(电子邮件、密码);
}
私有类AsyncLogin扩展了AsyncTask{
ProgressDialog pdLoading=新建ProgressDialog(admin\u login.this);
httpurl连接连接;
URL=null;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//此方法将在UI线程上运行
pdLoading.setMessage(“\t加载…”);
pdLoading.setCancelable(假);
pdLoading.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
//输入php文件所在的URL地址
url=新url(“http://www.uprtou.ac.in/and_login.php");
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
返回“异常”;
}
试一试{
//设置HttpURLConnection类以从php和mysql发送和接收数据
试一试{
conn=(HttpURLConnection)url.openConnection();
}捕获(IOE异常){
e、 printStackTrace();
}
conn.setReadTimeout(读取超时);
连接设置连接超时(连接超时);
conn.setRequestMethod(“POST”);
//setDoInput和setDoOutput方法描述了发送和接收的处理
conn.setDoInput(真);
连接设置输出(真);
//将参数附加到URL
Uri.Builder=新的Uri.Builder()
.appendQueryParameter(“用户名”,参数[0])
.appendQueryParameter(“密码”,参数[1]);
字符串查询=builder.build().getEncodedQuery();
//打开用于发送数据的连接
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
writer.write(查询);
writer.flush();
writer.close();
os.close();
连接();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
返回“异常”;
}
试一试{
int response_code=conn.getResponseCode();
//检查连接是否成功
if(response\u code==HttpURLConnection.HTTP\u OK){
//读取从服务器发送的数据
InputStream input=conn.getInputStream();
BufferedReader reader=新的BufferedReader(新的InputStreamReader(输入));
StringBuilder结果=新建StringBuilder();
弦线;
而((line=reader.readLine())!=null){
结果。追加(行);
}
//将数据传递给onPostExecute方法
返回(result.toString());
}否则{
返回(“未成功”);
}
}捕获(IOE异常){
e、 printStackTrace();
返回“异常”;
}最后{
连接断开();
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
//此方法将在UI线程上运行
pdLoading.disclose();
if(result.equalsIgnoreCase(“true”)){
/*登录成功后,在此启动另一个活动。如果您保持登录状态
使用Android的SharedReferences和注销按钮清除SharedReferences。
*/
Intent Intent=新的Intent(admin\u login.this,admin\u home.class);
星触觉(意向);
admin_login.this.finish();
}else if(result.equalsIgnoreCase(“false”)){
//如果用户名和密码不匹配,则显示错误消息
Toast.makeText(admin_login.this,“无效电子邮件或密码”,Toast.LENGTH_LONG.show();
}else if(result.equalsIgnoreCase(“异常”)| | result.equalsIgnoreCase(“不成功”)){
Toast.makeText(admin_login.this,“糟糕!出了问题。连接问题。”,Toast.LENGTH_LONG.show();
}
}
}
}
我的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:paddingTop="10dp"
android:layout_height="match_parent">
<!-- Email Label -->
<TextView
android:text="Username"
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textAlignment="center"
android:id="@+id/tv1" />
<EditText
android:text=""
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:padding="10dp"
android:background="@drawable/text_border"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<TextView
android:text="Password"
android:textSize="18dp"
android:layout_marginTop="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:id="@+id/textView3" />
<EditText
android:text=""
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="25dp"
android:padding="10dp"
android:background="@drawable/text_border"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView4"
android:password="true" />
<Button
android:text="Login"
android:textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
<Button
android:text="Signup"
android:textSize="18dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="checkLogin()"
android:id="@+id/button2" />
</LinearLayout>
现在什么也没发生,只显示布局。但当我点击登录按钮时,什么都没有发生。我已经试过很多次了…在布局中单击
onClick
标记。它应该是这样的:android:onClic