从android应用程序登录MySQL数据库不成功


Login not successful on MySQL database from android app

我创建了一个android应用程序,登录到MySQL数据库,但我没有收到任何结果。在backgroundWorker.java(函数"onPostExecute")警报对话框中的"结果"变量返回null,有人能解决这个问题吗?

BackgroundWorker.java "公共类BackgroundWorker扩展

 AsyncTask<String,String,String> {
    Context context;
    AlertDialog alertDialog;
    BackgroundWorker(Context ctx)
    {
      context =ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type=params[0];
        String login_url="192.168.10.9/login.php";
        if(type.equals("login"))
        {
            try {
                String user_name = params[1];
                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.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 = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    @Override
    protected void onPreExecute() {
       alertDialog=new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }
    @Override
    protected void onPostExecute(String Result) {
        Log.i("ok", "Result " + Result);
        alertDialog.setMessage(Result);
        alertDialog.show();
    }

CheckConn.java

public class CheckConn extends AppCompatActivity {
EditText user;
EditText pass;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check_conn);

    user = (EditText) findViewById(R.id.et_u);
    pass = (EditText) findViewById(R.id.et_p);
    Button press = (Button) findViewById(R.id.btn_login);
    press.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onLogin();
        }
    });
}
public void onLogin()
{
    String username= user.getText().toString();
    String password=pass.getText().toString();
    String type="login";
    BackgroundWorker backgroundWorker=new BackgroundWorker(this);
    backgroundWorker.execute(type,username,password);
}

}

connection.php

<?php
 $mysql_usernmae="root";
 $mysql_password="*****";
 $db="map";
 $db= new mysqli('localhost',$mysql_usernmae,$mysql_password,$db);
 ?>

login。

<?php
 require "connection.php";
 $user_name=$_POST["user_name"];
 $user_pass=$_POST["password"];
 $mysql_qry="select * from user_info where user_name like
 '$user_name' and user_password like 'user_pass';";
  $result=mysqli_query($db,$mysql_qry);
  if (mysqli_num_rows($result) > 0)
 {
  echo "login Successsss";
  }
else
{
echo "faileddddd Booooo";
}
?>

"

为什么不首先在浏览器中弹出login.php文件,硬编码user_name和user_pass的值,并检查连接是否在服务器端工作,

之后,你可以检查你的用户和通行证是否来自android应用程序,我必须假设你有一个数据库,在表中有一些数据,你正在进行查询,也检查数据库,表,查询的名称是否正确,你可以检查MySQL工作台,如果查询工作,并返回一些结果。