• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Android Studio MYSQL help

I need to connect to my database in phpmyadmin somehow, I've followed through this video

And even tried changing some things but when i try out my app so far on my phone, I just get the login status title. The php code works fine on my laptop itself. Can't try it on a Virtual Device since it's not compatible for my laptop, so I run it on my phone.

Here are the codes I have:

loginnn.php:


PHP:
<?php
require "smth.php";
$user_name = $_POST["username"];
$user_pass = $_POST["password"];
$mysql_qry = "select * from UserLogin where Email = '$user_name' and Pass = '$user_pass';";
$result = mysqli_query($conn , $mysql_qry);

if(mysqli_num_rows($result) > 0){
echo "Login Success";   
}
else{
echo "Login Not Success";
}

?>

smth.php:

PHP:
<?php
$db_name = "sailyx8096";
$mysql_username = "Sailyx8096";
$mysql_password = "thisiscorrectforsureanyway";
$server_name= "localhost";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);

?>


And then for Android Studio

AndroidManifest.xml:

Java:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="trogi.herbalifepro.POS">
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:usesCleartextTraffic="true">
        <activity android:name=".Login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Menu" />
        <activity android:name=".Registration"></activity>
    </application>

</manifest>

Login.java:

Java:
package trogi.herbalifepro.POS;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity {

    private EditText User;
    private EditText Pass;
    public Button Login;
    public Button Register;

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

        User = findViewById(R.id.ETUser);
        Pass = findViewById(R.id.ETPass);
        Login = findViewById(R.id.BTNLogin);
        Register = findViewById(R.id.BTNSIGNUP);



        Register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Login.this, Registration.class);
                startActivity(intent);
            }
        });

    }
    public void OnLogin(View view){
        String username = User.getText().toString();
        String password = Pass.getText().toString();
        String type = "login";

        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, username, password);
    }
}

and BackgroundWorker.java:


Java:
package trogi.herbalifepro.POS;

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.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class BackgroundWorker extends AsyncTask<String, Void, String> {
    Context context;
    AlertDialog alertDialog;

    BackgroundWorker(Context ctx) {
        context = ctx;
    }

    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://192.168.0.19/loginnn.php";
        if (type.equals("login")) {
            try {
                String username = 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("username", "UTF-8" )+"=" + URLEncoder.encode(username, "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) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

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


I've tried what I can, and I have this defense in two days or so and It would really help if anyone can figure this out, maybe there's something i haven't noticed, a useful line of code, anything!
 
Screenshots for clarification on my problem, it ends up like this regardless of whether or not it matches what's in my table.
 

Attachments

  • 42776230_148289742783427_105553610187735040_n.jpg
    42776230_148289742783427_105553610187735040_n.jpg
    29 KB · Views: 330
  • 42771270_385573261980265_3602717210398687232_n.jpg
    42771270_385573261980265_3602717210398687232_n.jpg
    28.7 KB · Views: 312
Looks ok.
To investigate this further, run the app in debug mode, and set a breakpoint in the doInBackground() method. Step through the code and see what data is returned from your remote login service. What value is in variable 'result'?
 
Looks ok.
To investigate this further, run the app in debug mode, and set a breakpoint in the doInBackground() method. Step through the code and see what data is returned from your remote login service. What value is in variable 'result'?

not sure if this is what you wanted me to do since i've never debugged before so im sorry;; but here are screenshots for before i hit login and after.

upload_2018-9-30_1-32-59.png


upload_2018-9-30_1-33-43.png
 
So when the code execution hits the breakpoint, what you can do is use the 'step over' button in the top header of that window, to step through each line of code, until you get to the part where it's reading data from the remote service. What data do you get back?

btw what happened to params[2], which should be the password. I don't see it in your variables window.
 
upload_2018-9-30_2-33-26.png


these showed up, just in case you need to see this, but then there's one thing that happened after i step down from this one. it skips and goes straight to here and this is what showed

upload_2018-9-30_2-34-2.png
 

Attachments

  • upload_2018-9-30_2-32-35.png
    upload_2018-9-30_2-32-35.png
    208.2 KB · Views: 249
Android debugging Lesson 1: Don't use e.printStackTrace(). This sends the message to standard output, you won't see it, and your app will fail silently. As you've clearly demonstrated here, the exception message was not shown to you.
Instead use Log.d(....) to show the Exception message, or just re-throw it. If you re-throw the Exception it'll be caught by the JVM. In either case, the Exception will show up in your Logcat in the form of a Stack Trace. If you don't know what that is, start reading here

https://androidforums.com/threads/please-read-me-before-posting.987318/

Now to your problem - it's most likely caused because your PHP server code is returning a malformed HTTP response message. Simply returning this

Code:
echo "Login Success";

won't work, because a HTTP response conforms to a specific format, involving headers.
I'll let you do some more research on how to construct a valid HTTP response message from your PHP server.
 
Reading on volley for now, thank you for that. but I do wonder, is that all that's the problem then? It doesn't affect the connection or anything, just the message request?
 
Back
Top Bottom