简体   繁体   中英

Login not successful on MySQL database from android app

I have created an android app that logins into a MySQL database but I don't receive any results. The 'result' variable in the alert dialog box in backgroundWorker.java (function "onPostExecute") returns null, can anyone solve this problem?

BackgroundWorker.java ` public class BackgroundWorker extends

 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

<?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";
}
?>

`

why you don't ejecute first your login.php file in the browser, harcode the values for user_name and user_pass, and check if the connection is working on server side,

after that you can check if your user and pass are coming from android app, I must assume you have a database with some data in the table you are making the query, also check if the name of database, table, query are correct, you can check with MySQL workbench if the query works and is returning some result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM