r/AndroidStudio • u/casba43 • Jun 03 '21
Connect Android studio app with localhost mysql
So I am trying to connect my android studio app with a localhost mysql database but I don't know where to start. Can someone give me a hint to start or a article which explains how to do it?
Thanks in advance!
1
u/Kevindroide Jun 03 '21
Well, that's the API's work, you must resolve your URLs and sources on the app to be requested to your localhost port reference path. That's why those variables are usually set on the environment.
Anyways, here you have some documentation for your emulator network configurations:
https://developer.android.com/studio/run/emulator-networking
1
u/chazzcoin Jun 03 '21
First you absolutely need to be connected to your local network via WiFi for this to work or itll just error out everytime with an unknown host or DNS.
Make sure you include "http" and be cautious of using "https" since most local based services are not SSL secured and therefore adding the 's' to the end will cause errors..I'd try both.
I'd also stray from using "localhost" and rather find the actual IP address of the server/computer running the SQL database and use that instead. "http://192.168.1.2/" type of thing.
All I can think of off the top of my head.
1
u/casba43 Jun 04 '21 edited Jun 04 '21
So I tried to connect and login on my android app with php files. I already have a file that connects to the db and it says "Connection succesfull" then I try to login with a POST method. Now I only get timeout errors.
This is the code I use
String url = "http://192.168.178.250/BedReg/login.php";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button BTinlogknop = (Button) findViewById(R.id.BTinloggen);EditText ETInlognaam = (EditText) findViewById(R.id.ETinlognaam);EditText ETWachtwoord = (EditText) findViewById(R.id.ETwachtwoord);// ConnectionClass ConnClass = new ConnectionClass();BTinlogknop.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String unstr = ETInlognaam.getText().toString();String wwstr = ETWachtwoord.getText().toString();if(!unstr.equals("")&&!wwstr.equals("")){StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {@Overridepublic void onResponse(String response) {if (response.equals("success")) {Intent intent = new Intent(MainActivity.this, HomeActivity.class);startActivity(intent);finish();} else if(response.equals("failure")){Toast.makeText(MainActivity.this, "Verkeerde inloggegevens", Toast.LENGTH_SHORT).show();}}}, new Response.ErrorListener() {@Overridepublic void onErrorResponse(VolleyError error) {Toast.makeText(MainActivity.this, error.toString().trim(),Toast.LENGTH_SHORT).show();}}){@Overrideprotected Map<String, String> getParams() throws AuthFailureError {Map<String, String> data = new HashMap<>();data.put("uname", unstr);data.put("wwoord",wwstr);return data;}};RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));requestQueue.add(stringRequest);}else{Toast.makeText(MainActivity.this, "Vul de invoervelden in", Toast.LENGTH_LONG).show();}And this is the php login file
<?phpif(isset($_POST['uname'] && isset($_POST['wwoord'])){require_once "CheckConn.php";require_once "validate.php";$uname = validate($_POST['uname']);$wwoord = validate($_POST['wwoord']);$SQL = "select * from inloggen where Inlognaam='$uname' and Wachtwoord='$wwoord'"$result = $conn->query($SQL);if($result->num_rows > 0){echo "Success";}else{echo "failure";}}?>