How do I connect an Android app and website with same database?

Akshay Narula's answer to I want my Android app to fetch data from an external WebApp. How should I go about it?

Do this, Write json files that you access on both the web and the Android app,

Use AsyncTask in android and access the url of your JSON to do the POST or GET with your data,

In the web, simply do the same, write a page that reads the json data that you return from the page you first created.

Android Example that reads data from a json file:

  1. private void parseJsonFeed(JSONObject response) {
  2. try {
  3. Log.e("Log1", "entered feed json pass");
  4. String result = response.toString();
  5. JSONArray feedArray = response.getJSONArray("feed");
  6. //JSONArray feedArray = new JSONArray(resul

Do this, Write json files that you access on both the web and the Android app,

Use AsyncTask in android and access the url of your JSON to do the POST or GET with your data,

In the web, simply do the same, write a page that reads the json data that you return from the page you first created.

Android Example that reads data from a json file:

  1. private void parseJsonFeed(JSONObject response) {
  2. try {
  3. Log.e("Log1", "entered feed json pass");
  4. String result = response.toString();
  5. JSONArray feedArray = response.getJSONArray("feed");
  6. //JSONArray feedArray = new JSONArray(result); for (int i = 0; i < feedArray.length(); i++) {
  7. JSONObject feedObj = (JSONObject) feedArray.get(i);
  8. FeedItem item = new FeedItem();
  9. item.setId(feedObj.getInt("id"));
  10. item.setName(feedObj.getString("name"));
  11. // Image might be null sometimes String image = feedObj.isNull("image") ? null : feedObj
  12. .getString("image");
  13. item.setImge(image);
  14. item.setStatus(feedObj.getString("status"));
  15. item.setProfilePic(feedObj.getString("profilePic"));
  16. item.setTimeStamp(feedObj.getString("timeStamp"));
  17. item.setVideoUrl(feedObj.getString("multimedia"));
  18. String theAud = feedObj.isNull("audio") ? null : feedObj.getString("audio");
  19. item.setAudiolink(theAud);
  20. // url might be null sometimes String feedUrl = feedObj.isNull("url") ? null : feedObj
  21. .getString("url");
  22. item.setUrl(feedUrl);
  23. feedItems.add(item);
  24. }
  25. // notify data changes to list adapater listAdapter.notifyDataSetChanged();
  26. } catch (JSONException e) {
  27. Log.e("Log1", e.getMessage());
  28. e.printStackTrace();
  29. }
  30. }

For accessing database of website to be used by android you need to create API (Restful or SOAP). This API will expose you the url on which someone hits, will return the required data from database in the form of JSON or XML.

To create API you need to write server side code. Let’s take that you have created website in Java/.Net/PHP then you need to create webservices in Java/.Net/PHP that will access data from database and expose that data onto the url. Suppose your website is website-domain.com then you can expose to

website-domain.com/api/getAllRecords

or you can create subdomain and expose the

For accessing database of website to be used by android you need to create API (Restful or SOAP). This API will expose you the url on which someone hits, will return the required data from database in the form of JSON or XML.

To create API you need to write server side code. Let’s take that you have created website in Java/.Net/PHP then you need to create webservices in Java/.Net/PHP that will access data from database and expose that data onto the url. Suppose your website is website-domain.com then you can expose to

website-domain.com/api/getAllRecords

or you can create subdomain and expose the api on that sub domain. To know how to create webservices /APIs in Java/.Net/PHP please go through google.

If your website is developed on wordpress, opencart and other open source tools then you need to add plugins that will expose API automatically

And on Android side you need to use these API using HTTP request and in response you will get a JSON string or XML String. Then you have to parse that string and show data as per your requirement.

Write an API/Rest layer for the database and let both the web and android app connect to the API/Rest layer.

In theory, there is nothing that stops you from doing so. And I have seen it working pretty good too. It can work seamlessly except of course if the traffic on either doesn't demand partitioning or scaling up the database.

The website would access the MySQL database normally via the connection string using mysqli, however, the android app instead posts data such as variables to a PHP script, the PHP script can then get the retrieved data and perform the mysql queries and json_encode the output and print the encoded data. The android app would then receive the outputted json format and the andr

In theory, there is nothing that stops you from doing so. And I have seen it working pretty good too. It can work seamlessly except of course if the traffic on either doesn't demand partitioning or scaling up the database.

The website would access the MySQL database normally via the connection string using mysqli, however, the android app instead posts data such as variables to a PHP script, the PHP script can then get the retrieved data and perform the mysql queries and json_encode the output and print the encoded data. The android app would then receive the outputted json format and the android app can then decode the json and process the data accordingly.

The link here would help you on the android part - How to connect Android with PHP, MySQL

Database connection string. Basically this is done through a lower level api using http to send data to the database.

Depending what language and what database you use, there are usually sdks made already for you. Most database support concurrent connection and have update locks built in.

Mostly probably Yes!
You’ll have to use either REST or SOAP API for the website and your app integration.

Choosing either one depends on what scale you’ll be using it.

REST API is more preferable and widely used, as it makes easy to use get and post method calls.

android cannot connect to sql connection string same as webapp .

you have to make rest base api or web services to get data for mobile apps

Some databases have cloud based api already

Traditional SQL, Oracle you need to make custom api to connect to database.

Most people use SQL lite for mobile which is a tiny db.

Wordpress uses a mysql database. So you can do it in several different ways. I recommend you to write a bridge in php that gets the data from the database and sends it to your app JSON encoded.

For example, your php file could look something like this for retrieving data:

  1. <?php mysql_connect("localhost","db_user","db_password"); mysql_select_db("database_name"); if(isset($_REQUEST['getrecords'])) getRecords($_REQUEST['id']); function getRecords($id){ $q=mysql_query("select * from test_ranking where id=$id"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close()

Wordpress uses a mysql database. So you can do it in several different ways. I recommend you to write a bridge in php that gets the data from the database and sends it to your app JSON encoded.

For example, your php file could look something like this for retrieving data:

  1. <?php mysql_connect("localhost","db_user","db_password"); mysql_select_db("database_name"); if(isset($_REQUEST['getrecords'])) getRecords($_REQUEST['id']); function getRecords($id){ $q=mysql_query("select * from test_ranking where id=$id"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close(); } ?>

Of course this is very insecure and you should write some extra code to make this script fit your needs. After doing this, you can simply make a httprequest from your android app and get the json data.

There is also Wordpress plug-in called JSON-API it gives you ability to access every object of Wordpress( post ,page ,category.. etc ) in the form of JSON. you can use any rest client like volley from google, retrofit from Square or build your won to access wordpress data.

install Wordpress plug-in http://wordpress.org/plugins/json-api/

Hope it helps!

You cannot directly connect android Application to external databases. You would need to use some form of webservice, may be a simple java webservice or php scripts, which may retrieve data from the database.

There are few Library files like K-Soap which will facilitate in sending and geting data from Web services.

There are lot of online Applications,which allows you to create online databases for free.One such website,which I liked the most is OpenShift - PaaS by Red Hat, Built on Docker and Kubernetes.

Openshift allows to create databases for Free and you can also host web services or Web Appl

You cannot directly connect android Application to external databases. You would need to use some form of webservice, may be a simple java webservice or php scripts, which may retrieve data from the database.

There are few Library files like K-Soap which will facilitate in sending and geting data from Web services.

There are lot of online Applications,which allows you to create online databases for free.One such website,which I liked the most is OpenShift - PaaS by Red Hat, Built on Docker and Kubernetes.

Openshift allows to create databases for Free and you can also host web services or Web Applications for free. If you like the service, you can pay and extend to a Premium account.

You’ll need to write an API which both the web app and mobile app talk to. Depending on how your backend is already set up, if you’re using a language like PHP or Ruby on Rails, there are a lot of gems and tools you can use to get your API set up.

One thing I recommend: when building an API, you’ll want to make sure you provide a way to authenticate against it (which will protect your user’s data as well as fight againt spam). This is commonly done by setting up some kind of token-based auth system whereby you send a token up with every request and verify that the token is valid.