How to Connect Android App to MySQL Database

Connecting Android Application to MySQL Database hosted on AWS

First of all, connecting to MySQL directly from an Android application is not secure, since you need the MySQL database access credentials for the app to communicate with the database.

This is why people recommend using a REST API Layer in between the MySQL database which you can implement using API Gateway and Lambda. This can be secured using a strong authentication mechanism for the API and using SSL for encrypting the messages at Transit.

However, this is not the case if you plan to connect to AWS DynamoDB from Android App. You can enforce Fine-Grained Access Permission to DynamoDB tables and roles using IAM policies.

Few Tutorials that might help,

  • Serverless REST API in Minutes with Serverless Framework.
  • Querying RDS MySQL with NodeJS Lambda Function.
  • Using Mobile Hub (To Simplify Things).
  • User Authentication with Cognito UserPools.

How can i connect to my MySQL database with an android app?

My class PutUtility for getData(), PostData, DeleteData(). you just need to change package name

package fourever.amaze.mics;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


public class PutUtility {

private Map<String, String> params = new HashMap<>();
private static HttpURLConnection httpConnection;
private static BufferedReader reader;
private static String Content;
private StringBuffer sb1;
private StringBuffer response;

public void setParams(Map<String, String> params) {
this.params = params;
}

public void setParam(String key, String value) {
params.put(key, value);
}

public String getData(String Url) {


StringBuilder sb = new StringBuilder();

try {
// Defined URL where to send data

URL url = new URL(Url);

URLConnection conn = null;
conn = url.openConnection();

// Send POST data request
httpConnection = (HttpURLConnection) conn;
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestMethod("GET");

BufferedReader in = new BufferedReader(
new InputStreamReader(httpConnection.getInputStream()));
String inputLine;
response = new StringBuffer();



while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception ex) { }
}

return response.toString();
}


public String postData(String Url) {


StringBuilder sb = new StringBuilder();
for (String key : params.keySet()) {
String value = null;
value = params.get(key);


if (sb.length() > 0) {
sb.append("&");
}
sb.append(key + "=" + value);
}

try {
// Defined URL where to send data

URL url = new URL(Url);

URLConnection conn = null;
conn = url.openConnection();

// Send POST data request
httpConnection = (HttpURLConnection) conn;
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestMethod("POST");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(true);
OutputStreamWriter wr = null;

wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(sb.toString());
wr.flush();

BufferedReader in = new BufferedReader(
new InputStreamReader(httpConnection.getInputStream()));
String inputLine;
response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

} catch (IOException e) {
e.printStackTrace();
} finally {
try {

reader.close();
} catch (Exception ex) {
}
}


return response.toString();
}


public String putData(String Url) {


StringBuilder sb = new StringBuilder();
for (String key : params.keySet()) {
String value = null;
try {
value = URLEncoder.encode(params.get(key), "UTF-8");
if (value.contains("+"))
value = value.replace("+", "%20");

//return sb.toString();


// Get the server response

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

if (sb.length() > 0) {
sb.append("&");
}
sb.append(key + "=" + value);
}

try {
// Defined URL where to send data

URL url = new URL(Url);

URLConnection conn = null;
conn = url.openConnection();

// Send PUT data request
httpConnection = (HttpURLConnection) conn;
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestMethod("PUT");
httpConnection.setDoInput(true);
httpConnection.setDoOutput(false);
OutputStreamWriter wr = null;

wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(sb.toString());
wr.flush();

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
;
String line = null;

// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb1.append(line + " ");
}

// Append Server Response To Content String
Content = sb.toString();


} catch (IOException e) {
e.printStackTrace();
} finally {
try {

reader.close();
} catch (Exception ex) {
}
}
// Send PUT data request
return Url;

}


public String deleteData(String Url) {


StringBuilder sb = new StringBuilder();
for (String key : params.keySet()) {

try {
// Defined URL where to send data

URL url = new URL(Url);

URLConnection conn = null;
conn = url.openConnection();

// Send POST data request
httpConnection = (HttpURLConnection) conn;
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setRequestMethod("DELETE");
httpConnection.connect();


reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line = null;

// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb1.append(line + " ");
}

// Append Server Response To Content String
Content = sb.toString();


} catch (IOException e) {
e.printStackTrace();
} finally {
try {

reader.close();
} catch (Exception ex) {
}
}



}
return Url;

}
}

And use this class like this

 @Override
protected String doInBackground(String... params) {

res = null;
PutUtility put = new PutUtility();

put.setParam("ueid", params[0]);
put.setParam("firm_no", params[1]);
put.setParam("date_incorporation", params[2]);
put.setParam("business_name", params[3]);
put.setParam("block_no", params[4]);

try {

res = put.postData(
"Api URL here");

Log.v("res", res);
} catch (Exception objEx) {
objEx.printStackTrace();
}

return res;
}


@Override
protected void onPostExecute(String res) {

try {

} catch (Exception objEx) {
mProgressDialog.dismiss();
objEx.printStackTrace();
}
}

Please use this. Hope it helps you in future also.
Check this if this is the problem

$u_contact=$_POST["contact"]"

here is the problem i think so brother. replace with

$u_contact=$_POST["contact"];

Android device can't connect to mysql in localhost

I have solved adding this on manifest...

    android:usesCleartextTraffic="true"


Related Topics



Leave a reply



Submit