使用PHP脚本在MySQL中确认用户名和密码后,如何在Android Studio中打开另一个活动?
我试图使用HTTP,但是由于我的SDK版本(28)而无法使用。所以,现在我正在尝试使用URL进行连接。
到目前为止,这是我的代码。
package com.example.myapp;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
public class Sign extends AppCompatActivity {
//Connector Variables
private static final String jsonurl = "http://192.168.1.2/login2.php";
private static TextView Info = null, Info2 = null, jSON;
String username;
String password;
String usern[], userp[];
int counter = 4;
EditText pas;
EditText usr;
Button create_account;
String ConnectionResult = "";
Boolean isSuccess = false;
private Button login;
private Context text;
private View view;
private String LoginSMessage = "Login successful";
String success = "succesful";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_sign );
Button login = findViewById( R.id.btnLogin );
usr = ( EditText ) findViewById( R.id.etUsername );
pas = ( EditText ) findViewById( R.id.etPassword );
Info = ( TextView ) findViewById( R.id.tvInfo );
jSON = ( TextView ) findViewById( R.id.tvJSON );
Button create_account = findViewById( R.id.btnCreate );
login.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
username = usr.getText().toString();
password = pas.getText().toString();
String type = "login2";
GetJSON json = new GetJSON(this);
json.execute(type, username, password);
}
});
}
class GetJSON extends AsyncTask<String, Void, String> {
public GetJSON(Object o) {
}
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.1.2/login2.php";
BufferedReader bufferedReader = null;
if (type.equals( "login2" )) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL( login_url );
HttpURLConnection con = ( HttpURLConnection )
url.openConnection();
con.setRequestMethod( "POST" );
con.setDoOutput( true );
con.setDoInput( true );
OutputStream outputStream = con.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter( new
OutputStreamWriter( outputStream, "UTF-8" ) );
String post_data = URLEncoder.encode( "user_name", "UTF-8"
) + "=" + URLEncoder.encode( user_name, "UTF-8" ) + "&" +
URLEncoder.encode( "password", "UTF-8" ) + "=" +
URLEncoder.encode( password, "UTF-8" );
bufferedWriter.write( post_data );
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = con.getInputStream();
bufferedReader = new BufferedReader( new
InputStreamReader( inputStream, "iso-8859-1" ) );
StringBuilder sb = new StringBuilder();
//bufferedReader = new BufferedReader( new
InputStreamReader( con.getInputStream() ) );
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append( json + "\n" );
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
return null;
}
@Override
protected void onPostExecute(String s){
super.onPostExecute( s );
jSON.setText(s);
Log.d("CREATION", "Execution is done"+s);
}
}
我希望我的代码在确认MySQL中的用户名和密码后能够打开另一个活动。
答案 0 :(得分:1)
您可以在服务器上编写以下PHP脚本:
$con=mysqli_connect("localhost","uname","pass","dbname");
if (mysqli_connect_errno($con)) {
echo "Error in connection : " . mysqli_connect_error();
}
$uname= $_POST['username'];
$pass= $_POST['password'];
$result = mysqli_query($con,"SELECT * FROM tableName where uname='$uname' and pass='$pass'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo "Login success";
}
mysqli_close($con);
现在在您的android应用中,您可以有一个简单的if条件
@Override
protected void onPostExecute(String s){
super.onPostExecute( s );
jSON.setText(s);
Log.d("CREATION", "Execution is done"+s);
if(s!=null){
Intent i = new Intent(your_current_activity_name.this,your_next_activity_name.class);
startActivity(i);
}else{
Toast.makeText(getApplicationContext(), "Login credentials wrong", Toast.LENGTH_LONG).show();
}
}