我试图连接我的android应用程序到mysql数据库。数据库连接通过php文件检查使用 Postman 。但我仍然无法连接android应用程序到mysql数据库。我不知道是什么原因,这是我的代码。停止运行应用程序。这里包括我的录音部分
数据库连接.php`
<?php
$DB_NAME ="garment";
$DB_USER ="root";
$DB_PASSWORD ="";
$DB_HOST ="localhost";
//Connecting to Database
$con = mysqli_connect($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_NAME);
if(!$con)
{
//echo "Connection Error..".mysqli_connect_error();
}
else
{
//echo"<h3>Database Connectrion success...</h3>";
}
?>
用户登录.php
<?php
require_once('dbConnect.php');
$username = $_POST["username"];
$password = $_POST["password"];
$sql = "select * from login where username like '$username' and password like '$password';";
$res = mysqli_query($con,$sql);
if (mysqli_num_rows($res)>0)
{
$row=mysqli_fetch_assoc($result);
$name=$row["username"];
Echo "Login success"
}
else
{
echo "failure";
}
?>
主活动.java
package com.example.gagana.garment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText usernametxt,passwordtxt,factoryname;
private Button login;
String username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernametxt=(EditText)findViewById(R.id.txtusername);
passwordtxt=(EditText)findViewById(R.id.txtpassword);
factoryname=(EditText)findViewById(R.id.factoryname);
login=(Button)findViewById(R.id.btnlogin);
}
public void userLogin(View view)
{
username = usernametxt.getText().toString();
password = passwordtxt.getText().toString();
String method="login";
Constants constants=new Constants(this);
constants.execute(method,username,password);
}
}
``` `Constants.java` ```
package com.example.gagana.garment;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class Constants extends AsyncTask<String,Void,String> {
AlertDialog alertDialog;
Context ctx;
Constants(Context ctx)
{
this.ctx=ctx;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Information.....");
}
private static final String Root_URL = "http://192.168.8.10/android/userLogin.php";
@Override
protected String doInBackground(String... strings) {
String method = strings[0];
if(method.equals("login"))
{
String username=strings[1];
String password=strings[2];
try {
URL url = new URL(Root_URL);
HttpsURLConnection httpsURLConnection=(HttpsURLConnection)url.openConnection();
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setDoInput(true);
OutputStream outputStream = httpsURLConnection.getOutputStream();
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data= URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream=httpsURLConnection.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();
httpsURLConnection.disconnect();
return response;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String s) {
alertDialog.setMessage(s);
alertDialog.show();
}
}
`
暂无答案!
目前还没有任何答案,快来回答吧!