gpt4 book ai didi

java - 使用 PHP 和 MySQL 的登录和注册显示 html 标签作为响应

转载 作者:行者123 更新时间:2023-11-30 22:22:47 26 4
gpt4 key购买 nike

我做了一个登录和注册 Activity 。
我无法登录也无法在数据库中注册。我将 Activity 中的数据分别发送到 login.php 和 Registration.php。我正在使用 wamp 服务器。

问题出在 BackroundTask.java 中,其中响应作为 Html 标记从服务器返回。如何从 login.php 和 registration.php 的回显消息中消除它们

背景任务.java

public class BackgroundTask extends AsyncTask<String, Void, String> {

// AlertDialog alertDialog;

Context ctx;
BackgroundTask(Context ctx)
{
this.ctx=ctx;

}


@Override
public String doInBackground(String... params) {


String reg_url="http://10.0.2.2/webapp/Register.php";
String login_url="http://10.0.2.2/webapp/login.php";
String method= params[0];
if(method.equals("register"))
{
String name=params[1];
String u_address=params[2];
String user_pass=params[3];
String u_email=params[4];
String u_zip=params[5];
String u_mob=params[6];

try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("address","UTF-8")+"="+URLEncoder.encode(u_address,"UTF-8")+"&"+
URLEncoder.encode("user_pass","UTF-8")+"="+URLEncoder.encode(user_pass,"UTF-8")+"&"+
URLEncoder.encode("emailid","UTF-8")+"="+URLEncoder.encode(u_email,"UTF-8")+"&"+
URLEncoder.encode("zip","UTF-8")+"="+URLEncoder.encode(u_zip,"UTF-8")+"&"+
URLEncoder.encode("mob","UTF-8")+"="+URLEncoder.encode(u_mob,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

String result;
result = bufferedReader.readLine();
// / InputStream is= httpURLConnection.getInputStream();
// is.close();
// return "Registration Success...";
return result;
}
catch(Exception e)
{
return null;
}
/*catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}

else {
if (method.equals("login")) {
String login_name = params[1];
String login_pass = params[2];
try {
URL url = new URL(login_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("login_name","UTF-8")+"="+URLEncoder.encode(login_name,"UTF-8")+"&"+
URLEncoder.encode("login_pass","UTF-8")+"="+URLEncoder.encode(login_pass,"UTF-8");

bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();

InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response="";
String line="";
while ((line=bufferedReader.readLine())!=null)
{
response+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return response;

}
catch(Exception e)
{
return null;
}
/*catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}
}

return null;
}

@Override
protected void onPreExecute() {
// alertDialog=new AlertDialog.Builder(ctx).create();
// alertDialog.setTitle("Login information...");
super.onPreExecute();
/* if(response.equals("Login Success...")){
Toast.makeText(ctx,response,Toast.LENGTH_LONG).show();
}
else if(response.equals("Login Failed!Try again")) {
Toast.makeText(ctx,"Login failed!",Toast.LENGTH_LONG).show();
//alertDialog.setMessage(log);
//alertDialog.show();
}
*/
}


@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String result) {
/* if(result.equals("Registration Success...")){
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
else{
//if(result.equals("Login Success...")){
// Toast.makeText(ctx,"Login Success!",Toast.LENGTH_LONG).show();
alertDialog.setMessage(result);
alertDialog.show();
}
/*else
{
Toast.makeText(ctx,"Failed!",Toast.LENGTH_LONG).show();
} */

/* if(method.equals("login"))
{ */
if(result.equalsIgnoreCase("Login Success..."))
{

Intent intent = new Intent(ctx,MainActivity.class);
// intent.putExtra("USER_NAME",login_name);
ctx.startActivity(intent);

}


/* else{
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
else if(method.equals("register")) {
*/
else if (result.equals("Registered Successfully")){
Intent intent = new Intent(ctx,Login.class);
ctx.startActivity(intent);
}
else{
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
// }
}

}

在登录 Activity 中,当我尝试使用正确的值登录时,我看到一 strip 有 html 标签的 toast 消息

Login page

登录页面如上所示。

在注册页面填写值后,我只看到 <br> Toast中的标签如下图

Registration confirmation

为了引用,我附上了我的代码

初始化文件

<?php
db_name="citizen";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";

$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);

if(!$con)
{
echo"Connection Error...";
}
else
{
echo"Database connection Success...";
}

?>

登录.php

<?php
require"init.php";

$user_name=$_POST["login_name"];
$user_pass=$_POST["login_pass"];

//$sql_query=mysqli_query($con,"SELECT * FROM reg WHERE 'Email' = '$user_name' AND 'Password'='$user_pass'");
$sql_query="SELECT Name FROM reg WHERE Email like '$user_name' AND Password like '$user_pass';";
$result=mysqli_query($con,$sql_query);
//$row=mysqli_fetch_array($result);
//$data=$row[0];
if(mysqli_num_rows($result)>0)
//if($data)
{
echo"Login Success...";
}
else
{
echo"Login Failed!Try again";
}
//mysqli_close($con);
?>

注册.php

    <?php
require 'init.php';
$name=$_POST["user"];
$address=$_POST["address"];
$user_pass=$_POST["user_pass"];
$emailid=$_POST["emailid"];
$zip=$_POST["zip"];
$mob=$_POST["mob"];
$sql_query="INSERT INTO reg VALUES('$name','$address','$user_pass','$emailid','$zip','$mob');";

if(mysqli_query($con,$sql_query))
{
echo"Registered Successfully";
}
else
{
echo"Registration Failed";
}

?>

代码如下

登录.java

public class Login extends AppCompatActivity {
EditText ET_NAME,ET_PASS;
String login_name,login_pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ET_NAME=(EditText)findViewById(R.id.user_name);
ET_PASS=(EditText)findViewById(R.id.user_pass);

}


public void userLogin(View view)
{
login_name=ET_NAME.getText().toString();
login_pass=ET_PASS.getText().toString();
String method="login";
if (!isValidEmail(login_name)) {
ET_NAME.setError("Invalid Email");
}

if (!isValidPassword(login_pass)) {
ET_PASS.setError("Invalid Password");
}
else {
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, login_name, login_pass);
}
//Intent getIssues= new Intent(this,MainActivity.class);
//getIssues.putExtra("MA", );
//this.startActivity(getIssues);
}
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}

// validating password with retype password
private boolean isValidPassword(String pass) {
if (pass != null && pass.length() > 6) {
return true;
}
return false;
}
public void userReg(View view)
{
startActivity(new Intent(this, Registration.class));
}
}

注册.java

public class Registration extends Activity
{

EditText ETname, ETrpassword, ETpassword, ETaddress, ETemail, ETzip, ETmob;
String name, address, userpass, email, ruserpass, zip, mob;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);


ETname = (EditText) findViewById(R.id.editText);
ETaddress = (EditText) findViewById(R.id.address);
ETemail = (EditText) findViewById(R.id.editText3);
ETpassword = (EditText) findViewById(R.id.passwordfield);
ETrpassword = (EditText) findViewById(R.id.editText4);
ETzip = (EditText) findViewById(R.id.editText2);
ETmob = (EditText) findViewById(R.id.editText5);
Button reg = (Button) findViewById(R.id.regbutton);

}

public void reg(View view) {
name = ETname.getText().toString();
address = ETaddress.getText().toString();
userpass = ETpassword.getText().toString();
ruserpass = ETrpassword.getText().toString();
email = ETemail.getText().toString();
zip = ETzip.getText().toString();
mob = ETmob.getText().toString();

String method = "register";

boolean cancel = true;


if (TextUtils.isEmpty(name) || name.length() < 3) {
ETname.setError(getString(R.string.error_validname_required));
cancel = false;
}


if (TextUtils.isEmpty(address) || address.length()<15) {
ETaddress.setError("Invalid address");
cancel = false;
}

if(ruserpass.isEmpty())
{
ETrpassword.setError(getString(R.string.error_field_required));
cancel=false;
}
if (TextUtils.isEmpty(userpass) || userpass.length()<6) {
ETpassword.setError("Fill atleast 6 characters");
cancel = false;
}
if(!userpass.equals(ruserpass))
{
Toast.makeText(getApplicationContext(),"Password mismatch",Toast.LENGTH_SHORT).show();
cancel=false;
}

if (!isValidEmail(email)) {
ETemail.setError("Invalid Email");
cancel = false;
}
if(zip.length()!=6)
{
ETzip.setError("Invalid PINCODE");
cancel = false;
}
if(mob.length()!=10)
{
ETmob.setError("Invalid number");
cancel = false;
}

/* boolean cancel = false;
View focusView = null;

ETname.setError(null);
if (TextUtils.isEmpty(name)) {
ETname.setError(getString(R.string.error_field_required));
focusView = ETname;
cancel = true;
}

ETpassword.setError(null);
if (TextUtils.isEmpty(address)) {
ETaddress.setError(getString(R.string.error_field_required));
focusView = ETaddress;
cancel = true;
}


ETaddress.setError(null);
if (TextUtils.isEmpty(address)) {
ETaddress.setError(getString(R.string.error_field_required));
focusView = ETaddress;
cancel = true;
}

if (name.length() < 3) {
ETname.setError(getString(R.string.error_validname_required));
focusView = ETname;
cancel = true;
} */
// }


else if(cancel==true){
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method, name, address, userpass, email, zip, mob);
finish();
}
}
private boolean isValidEmail(String email)
{
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
}

即使在警告框中,我也会看到相同的 html 标签。我是 android 的初学者,我还在学习它。请帮我解决这个问题。欢迎提出任何建议。

最佳答案

我在 init.php 中犯了一个愚蠢的错误更正后的代码是

<?php
$db_name="citizen";
$mysql_user="root";
$mysql_pass="";
$server_name="localhost";

$con=mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);

if($con)
{
echo"Connection success...";
}
else
{
echo"Connection failed...";
}

?>

关于java - 使用 PHP 和 MySQL 的登录和注册显示 html 标签作为响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36219023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com