Trying to understand the MySQL/PHP/JSON/Restful/Android relationship - php

I'm making an android app that will have a backend database (MySQL).
From my research, it seems like one of the ways to connect to a MySQL database is to use PHP to make an HTTP request to the database, which will then return data in the form of a JSON object which is decoded.
PHP is used to make a GET/POST to the database (often using a service like Retrofit)
The database fetches or posts the appropriate data and must alert the client/return data
The database sends back information that is JSON form
The client decodes the returned JSON
Is this the correct logic?
Why is PHP needed to make the request? Is this an acceptable way of interacting with a MySQL database from an android app? Do you have any advice to offer for going about this?

If your are using the LAN MYSQL server in your own device, you can use MYSQL command to connect the server
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://"+serverip+":"+serverport+"/"+dbName;
Connection conn = DriverManager.getConnection(url,username,password);
String queryString = "SELECT * FROM materialdb";
PreparedStatement statement = conn.prepareStatement(queryString);
ResultSet rss = statement.executeQuery(queryString);
results = new ArrayList<FindDetails>();
rss.beforeFirst();
while(rss.next())
{
FindDetails item_details = new FindDetails();
item_details.setMaterial_Code_No(rss.getString("Material_Code_No"));
results.add(item_details);
}
rss.close();
statement.close();
}
catch(Exception e)
{
Toast.makeText(this, "Fail to get main list", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
If the server is on web, the better way of connect to server is PHP

This approach looks like it is for a web server, not a client (much less an Android client). Usually, servers have databases and clients are "consumers" of that data. However, clients also usually have databases to store that data.
PHP is a server-side technology whereby you can use a URL and HTTP to send and retrieve data. PHP can then be used to connect to a database to provide rich content to the client.
In Android, your device (likely) will not have a URL or website. It can have a database and you can use HTTP libraries (like Retrofit - http://square.github.io/retrofit/) to get data from websites (that might use HTTP). But this is passive - your client will have to request data, the server cannot "push" data to it.
For "push" data, you can use Firebase (Google Cloud Messaging) which uses a Google framework and API to have a (reasonably) constant connection to the device to "push" notifications to it. If you follow their rules, you will send a small data packet that tells your client to wake and start making specific HTTP requests to whatever URL you like.

Related

How to receive data from one server to another using PHP

I want to get JSON data from an server and store it to database on my server.
I bought API that returns json, which means that server is not mine but I want to take that data and store it to my server.
How to call API, receive data and store it to my server?
P.S. That API only allows access from 5 IP addresses and I want to use it for android app. Instead of getting data directly from that server to android phone, I have to store that data to my server and then call GET method on my server to receive results on the android phone. This way I could even use GCM.
It doesn't have to be php script. If you have better idea or solution please let me know.
You could use
$json = json_decode(file_get_contents("http//example.com/path?query"));

Is it possible to pull data from HTML5's local storage and save to server database?

I have an idea for a social web app and part of the idea is to pull data from localstorage onto the server database. The reason for this is say you have an idea and you want to put it on your next status for this social app, but you have no signal and wireless anywhere. You write out the status on the app and it saves it to the localstorage for use when they get back to somewhere where they can connect their phone online.
They then get to a place where they can go online, the app then detects that there is a change in the localstorage and then pulls it from there and saves to the server database.
Is this at all possible, if so how would you go about implementing it?
If it's not at all possible, do you think this could be sometime in the future?
I look forward to hearing from your comments and answers on this one as I think it could be quite a big discussion.
Yes it's possible, why wouldn't it be? When you pull data from the local storage, the data works just like any other Javascript variable, there are no restrictions that would stop you sending it to a server other than the lack of Internet connection.
how would you go about implementing it?
First you'd need to detect the connection status, see this question. So when a user tries to update their status, it checks if the connection is online and if it isn't then save it to local storage.
Use the methods in the linked question to detect when the connection comes back up, and at that point pull the data from local storage and send it to the server via ajax, then clear the local storage to prevent duplicate data being sent.
You can periodically check navigator.onLine in your Javascript to get the online status of a device. Note that while
navigator.onLine == false
safely tells you that a device of offline,
navigator.onLine == true
doesn't necessarily mean it has access to the internet and can perform the request, just that it is connected to some sort of network.
Once you assume that the device is not offline you'd send an ajax request (I recommend using jQuery's $.ajax function) in which you POST the data from your localStorage
(localStorage.getItem('dataToPull'))
to a PHP script, which then inserts it into your MySQL database.
If that ajax request fails, and you're sure it's not getting a PHP/MySQL error, it's safe to assume that although the device is connected to a network and navigator.onLine is true, there's no internet connectivity. You can then do some error handling, e.g. poll the device again in 5 minutes.

Contact sync with android and php

I need to send all my contacts to server and return back only the contacts that are using my app.I am using HttpPost for sending data to the server. I am thinking of sending it as an string separated by delimiters or as NameValuePair. Is there any better way to do contact sync?
Are you using a database? If so create a php webservice that connect to your database, make a query and return results according the criteria you need. then you need to encode the result in a json array and use json to connect your android app and get the results, here is an example how to use json with android:
http://www.vogella.com/articles/AndroidJSON/article.html
There are many more libraries you can use: gson/json/jackson... all similar, you can make a search on google and chose the one you like better
Instead of json you can use SOAP, I have never used it but you can try : http://code.google.com/p/ksoap2-android/
Direct database connection is not recommended on android

making database available to other servers

OK,
I'm not entirely clear how to ask this question, so please feel free to edit it.
Here's the idea.
I have a database that I think may be useful to webmasters.
I would like to offer, as a paid service, the inclusion of some interface with the database.
I want my database to dynamically populate drop-down option menus.
I understand how to do it when the database, requesting html/javascript and the processing php are all on the same server.
I'm not so sure whether this can be done if the requesting html html/javascript are on the webmaster's server, and the processing phps and the database are on my server.
I'm also not sure how to make sure that only authorized users can hook up to my database.
At this point, I am looking for a macro-level solution, not the code implementation.
Thanks!
Instead of directly connecting to remote database server, I suggest you to create a simple web service (such as a PHP page grabbing information from database, based on the request stated in query string you specified ) to generate XML / JSON for your web page.
Your web page, containing your drop down box, can make an AJAX request to fetch the XML & parse for data. With this mechanism, security problem caused by exposing the database can be avoided.
My approach to this would be to write a web service (whether SOAP, XMLRPC or REST) that requires the users to submit an API key with each request. The service would validate the key, talk to the database, and return results to the user in some standard format. The API key would (obviously) be unique for each user/subscription.
You could return the results of the request to the php script in JSONP format (json with padding). That allows the javascript to execute a callback method on the original webpage even though the service is on another domain.
You'd probably need some way of tracking state in your php code to determine if they're authenticated. One way would be to have the author of the 3rd-party webpage send an authentication token during their body onload to your php script. You could then mark the IP address of the client as "authenticated" and then all other requests from that same IP would return a valid result. Requests from a non-authenticated IP could return an error message.
IPs are obviously not unique in our world of NAT (network address translation). If you want it to be more fool-proof, you could generate your own token inside the PHP & send it back from the request to authenticate. The 3rd-party site would store that token in memory (or a cookie) & send it back to you on every request. You'd use that token as the proof of authentication on future requests, rather than the IP. This is basically how most servers do Sessions, PHP probably has something built in for that.

download database to iphone

The app I'm writing needs to get information from a remote database and then store it in the SQlite database on the iphone for offline use. As of now I'm using a php script to query the database and then just basically scrapping the html data from the php echo calls.
My problem is that now that I'm moving away from test data I'm using database tables that are fairly large with complex dependencies. I would really like to not to have to write a parser for these complex files to manually put the data and dependencies into the iphone database.
My question is if there is any way to use a php script to pass to the iphone a copy the actual database file and then just dump that into the SQlite database on the iphone.
While I'm not familiar with iphone development, it seems to me that you ought to be able to just construct the sqlite database file on the server, and then send it to the device (or have the device pull it down). Of course, this is only efficient if you need to send the whole database every time.
If you need to do things more incrementally (you're just adding some new data to the device's database) why not just have PHP write out SQL statements to make any necessary changes, send those to the device, and have the app execute them?
Make the serverss response XML and bring it into core data that way.
http://code.google.com/p/touchcode/wiki/TouchXML will help you parse the response.
You could also make the server send its response in JSON and use this library, http://code.google.com/p/touchcode/wiki/TouchJSON

Categories