This question already has answers here:
Can an Android App connect directly to an online mysql database
(8 answers)
Closed 6 years ago.
I have a website already setup which uses mysql database. I want to know how can i connect my app to that database.
What i wanna achieve is that my app should make a request to find a table of a defined "ID" or name. The table contains links to images or image names. I want the app to retrieve those images and display them on the android app.
What are the ways this can be done?
Can I use PHP to develop an android app?
Android does not support MySQL out of the box. The "normal" way to access your database would be to put a Restful server in front of it and use the HTTPS protocol to connect to the Restful front end.
Have a look at ContentProvider. It is normally used to access a local database (SQLite) but it can be used to get data from any data store.
I do recommend that you look at having a local copy of all/some of your websites data locally, that way your app will still work when the Android device hasn't got a connection. If you go down this route then a service can be used to keep the two databases in sync.
The one way is by using webservice, simply write a webservice method in PHP or any other language . And From your android app by using http client request and response , you can hit the web service method which will return whatever you want.
For PHP You can create a webservice like this. Assuming below we have a php file in the server. And the route of the file is yourdomain.com/api.php
if(isset($_GET['api_call'])){
switch($_GET['api_call']){
case 'userlogin':
//perform your userlogin task here
break;
}
}
Now you can use Volley or Retrofit to send a network request to the above PHP Script and then, actually the php script will handle the database operation.
In this case the PHP script is called a RESTful API.
You can learn all the operation at MySQL from this tutorial. Android MySQL Tutorial to Perform CRUD.
Yes you can connect your android app to your PHP to grab results from your database. Use a webservice to connect to your backend script via ASYNC task and http post requests. Check this link for more information Connecting to MySQL
Use android vollley, it is very fast and you can betterm manipulate requests.
Send post request using Volley and receive in PHP
Basically, you will create a map with key-value params for the php request(POST/GET), the php will do the desired processing and you will return the data as JSON(json_encode()). Then you can either parse the JSON as needed or use GSON from Google to let it do the parsing.
Related
Is it possible for an android application to check and update itself from phpmyadmin server?
You can do this with php. Use PHP to make a connection with the database and get Data from phpmyadmin database. Then make a restful API with PHP to return you all fetch data.
Restful API helps to create, read, update or delete information between different applications over the internet or HTTP protocol.
Now through your android app call this API and update your app.
you can follow this for more information.
Rest API in PHP
I'm learning to build android apps using java & android studio.
For my app i need internet connection and a server side database, I'm already php developer so i tought to build a PHP web API and by json, send and recieve data from specific URL.
For example, any user that will register inside my app, i can send his data to:
(via POST or GET method.)
example.com/api/user_register.php
Is it secure? How can i get data and send to an existing domain in java?
I'm looking for something similliar to file_get_content() or CURL in PHP.
Also i need to build something like push notifications..
Creating an web API to access a MySQL Database for Android is expected as there is no direct connection to the database possible on Android.
I've done something similar with one of my own apps. How secure it is depends on how you make it and where your PHP API is. I.e. is the data being sent to/from HTTP or HTTPS. In my own app, I encrypt the post data on the device, post it to my PHP API which decrypts and processes the request.
It then builds a response and json_encodes it, which is then encrypted and sent back to the device, where the device decrypts it and processes the JSON result. All of this is also done over HTTPS so hopefully, its fairly secure.
I'm developing a knowledge base java application, where I can store and retrieve annotations with its title, date when the note was created (SQL datetime), content, tags about the annotation, etc.
It can be done easily with a database (I'm using SQL Server 2014), but the main problem is that the server is running on my own PC and it has to be always on and running the SQL Server. Also, I would like to extend the application by storing and retrieving this kind of data on mobile apps for Android and iOS.
Is there any other way to store that type of data in some files so it can be uploaded to some cloud storage like Dropbox ? After storing it on Dropbox, all I would have to do is sync the app with dropbox, get the files and read/write stuff.
UPDATE: Thanks for all the answers they helped me a lot. The best solution for me is to replace SQL Server with SQlite, as Gabe Sechan commented. Now I can make changes on the database without the need of a server running 24/7 and I can use the same database on Android and iOS apps.
You can use just a basic ajax call to pull content from a Dropbox "public" URL.
function(contenturl,intoselector,callback){
if (contentwindow.currenttopic!==contentID){
jQuery.ajax({
type:'GET',
url:'//www.corsproxy.com/'+contenturl,
dataType:'text',
async:true,
success:function(data){
intoselector.html(data);
if (jQuery.type(callback)==="function")
callback();
}
});
}
Notice that this example pulls through corsproxy so that you don't receive any XSS errors, so the url you pass needs to not contain a protocol itself.
If you want to pull a JSON or XML string that is stored in the file, then you might need to play around with the dataType and contenttype options in the ajax call.
This can also be done using Google spreadsheets:
Reading:
Create a spreadsheet and publish it on the web
Use one of the many available Javascript libraries for pulling data from Google spreadsheets:
http://jlord.us/sheetsee.js/ (which uses Tabletop.js)
http://chriszarate.github.io/sheetrock/
Writing:
You can use a Google app script for writing to the spreadsheet (reference) OR
You can create a Google form linked to the spreadsheet and simply fill the form from your mobile app whenever you want to add some data to the sheet (reference)
Of all the cloud services, when it comes to Android, Dropbox's Sync API is one of the easiest to implement. Do you need specific code examples on how to sync with Dropbox?
Any tutorials out there on how to send and received data to and from a MySQL database hosted on a server using cPannel? I'm trying to make an android app that connects to the internet, and it needs to read and write data to and from the hosted database. The database is a MySQL one and is hosted on godaddy. I've been on the internet all day today looking, so any help would be greatly appreciated.
Well you need to write a webservice which will get the data from your mysql database and send it to your android app.
First of all you need a server which you have already. Create space to write php scripts there to interact withe the database that is hosted there.
Second Write a php script on your server which can retrieve the required data from the MySQL database. Encode the retrieved data inside a JSON object and send it back to your app. From your app you have to call the URL where the webservice is hosted, and the php script will do the interaction with the database, retrieve the results and send it back to your app in JSON.
Third - Follow the same method to send and retrieve data to server. Create multiple webservice as per your requirements.
This tutorial excellently illustrates what you are looking for. This example uses a local server. You can skip some of the steps there. Hope it helps. URL: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
If you still got any questions, comment below. I will be happy to help.
Thanks!
First, I'd like to let you know my experience.
PHP - medium | Android/Java - medium | JSON - low | MySQL - low
I'm trying to make an app that will be able to connect to a MySQL DB, grab a specified row, and display it in my application.
I've been doing a lot of research, but feel like I still have a lot of unanswered questions. From what I read that doing a direct connection to the database should never happen. Is this true? Next, most people suggest writing something in php to connect the app to the database. My main question is where do I put this php code? On a website or in the android app (if that's even possible)?
If anyone has experience with this, do you think I should follow this tutorial? (I don't want to buy a domain if I don't have to)
http://www.helloandroid.com/tutorials/connecting-mysql-database
Client side:
and android app that basically uses the defaulthttpclient in java to establish connection to a php page hosted on a website by posting data to it(httppost).
and a json parser to read the retrieved data and display it.
server side:
a php script on the web server which accepts post data from the client, and retrieves data from MySQL database at the server and displays it, which can be read by the client as an httpresponse.
i hope that helps.
Create a web service to pull objects that contain data from your database. A WCF Service is a good choice.