RESTful service with Android - php

I can't seem to get a grasp on getting the web-to-android concept. I was reading a bunch of stuff about RESTful services and JSON, but I never found anything that could help me. My idea is to have Access database somewhere online, and get files from it on the android app.
Where should I even begin? Say I got the DB and i got the server to put it on. Where does JSON come in play? Where are queries put into, some PHP, JSON, app itself?
As far as I could understand from all I have read, the connection should be somehow like this
WEB -- Database -- REST -- App
How does it connect to PHP, where does the PHP file go? I have seen many topics here talking about it, but everyone has already made SOME progress, I don't know where to even start. Could you direct me to some book worth reading?
Thank you

In simple JSON (JavaScript Object Notation) is a lightweight format that is used for data interchanging. see here for information.
You will have two ends for your project, the front end (android side) and the back end(the server & php). JSON is the way the two ends talk.
You asked how does it, i assume you mean the java code for the android side, connect to the PHP.
The PHP is the language you are writing your API for JSON requests in.
How does your java code use the api?
You use REST and made HTTP POST or GET requests depending on your need.
This article shows how to connect them together.
EDIT
The overall structure is the following:
-- Create db
-- Put db on server
-- create PHP webservice to fetch from db and encode in JSON
-- use REST to make HTTP requests to get data from the database
-- data comes back in JSON
-- decode JSON (using standard librarys in java)
-- use your data
Hopefully this helps
Thank you

Related

Accesing php web service and MySQL from Qt/C++ in android. How to send data to service?

I am pretty new with this. I am trying to develop some android application in Qt/Qml to query and update data to a database.
The problem I'm trying to solve:
There is a server with a MySQL database. The android application request some data. I use a very very basic php file that query in the database, format an XML and show that file starting with a header('Content-type: text/xml'); sentence, then some echo "<...>" sentences. This php file receives parameters via $_GET array and performs the query.
This is a little archaic but works fine. My client application use Qt/QML for android. It uses XmlListModel to obtain the data from the server and show that data in a ListView.
So far so good, but here is my real problem:
The user of the android application can change some data, and the modified data must be sent back to the server, who performs an update in the database.
My first attempt was to create another php file and send the data in the URI, get it via $_GET and perform the update to the database. This works but there could be many variables and the URI will become very large. I think might be better to send an XML (or JSON) from the client to the server, and put in that file the data for the update.
So, this is the question: How can I send an XML file from Qt/QML to the server? How does the php file obtain that data? Or maybe there is another, better way for doing this.
Every example I find just shows the server-to-client part, or is written in Java, and I don't know java enough.
Additional notes:
After I can solve this I will fight with security: SSL, avoiding SQL injection or anything, but that will be in another question if necessary.
I didn't use SOAP or similar because the first ideas was simple. Also, I have never used SOAP before. If this is the right way, I would be very grateful if you show me how to use it from Qt/QML in android.
Of course, I can use C++ for the client application since it is using Qt.
I use Qt since I already know it and the app might be useful in desktop as well.
Sorry if I made any english mistake.
Thank you in advance.

Getting data from MYSql into my iOS app with JSON

Ok I found a few questions on how to get data from a MYSql database into an iOS app, but I am just asking a few best practices here. I know these can all be separate questions, but I am hoping that they can be answered in a way that they relate to each other.
Am I correct to understand that to be able to get data into an iOS app - I need to first generate a JSON file, have that stored on a server and than have the app download this file??
If the previous answer is NO then does that mean, I can pull in data on the fly?
Lastly I have seen PHP examples to create JSON files, but iOS is in Objective-c. Does this mean I need to load a UIWebView to be able to load the PHP page that generates the file?
What I have:
I have a MYSql database - it is set up through PHPMyAdmin, so I am not familiar enough with the creation process of the database yet. I will look into that.
I can also export the JSON file from PHPMyAdmin, but that is no good to me in a iOS app.
I also have the parsing from a JSON file into an iOS app sorted, but I want to be able to do this on the fly instead of creating potentially hunderds of files.
I hope someone can help me here:-)
I am not necessarily asking for code, but would be mad to ignore it:-)
The problem is that there are not any iOS libraries for directly connecting to a MySQL server; and you really wouldn't want to do that, anyway. So, you need an intermediary server capable of sending data in a format your iOS application can understand. Note, this does not mean the data has to be JSON formatted. But it is very easy to use JSON as the format for your data. Most languages have native support for generating JSON from its native object format(s).
Once you have a server capable of sending data in your preferred format, you need to write some way for your iOS application to retrieve it. You do not have to use a UIWebView for this. As mentioned, the NSURLConnection framework is very easy to use to make such a request. However, there are a lot of other factors to consider when making network requests and others have already done most of the work for you. I like using the AFNetworking framework in conjunction with JSONKit. AFNetworking makes asynchronous calls to remote web services very easy, and JSONKit is nicer than NSJSONSerialization in my opinion.
What I do to retrieve data from MySQL to my iOS app is:
Create a PHP file on your server and prepare it for GET methods (you're going to send data from the iOS app)
Send a request from your iOS app to your php file, like: "www.yourdomain.com/data.php?name=..."
Process the information on your php file and echo the json output.
When connectionDidFinishLoading: convert the NSData to an Array using NSJSONSerialization.
Do whatever you like with the output information
That's just do way I do. I'm not familiar with other approaches.
PHP (and any other server side language) can take the data from the MySQL database and output it to any client as JSON, on the fly. No need to save the JSON to disk beforehand. Of course, from the client's point of view, there really is no fundamental difference (except the data will always be the latest representation of what's in the database).
You also don't have to use a UIWebView. There's a number of ways to make an HTTP request using Objective-C, but you'll likely want to look at something along the lines of NSURLConnection's sendSynchronousRequest:returningResponse:error: method (I prefer using synch methods inside an async block, but that's not the only way). You can find many tutorials on how to do similar things, as well as higher level libraries to simplify the process.

Android fetch data from database

I've decided to try out some DB connections for my android applikation. However I need some advice regarding structure.
I came to know that in order to fetch data from DB with Android I need to use php scripts. I'm a total novice in this area and for me it sounds a bit akward. Therefore I figured I could create a Java server which the application connects to, and among other things this server also fetches data from the DB and returns.
In a performance perspective, what's best? Let the application itself fetch data from the DB, or connect to the server which fetches it for you? How about security? For clearance, I will have a Java server anyhow to take care of other things.
Sorry about the Vista paint skills.
Thanks for any input!
Like others have said it doesn't matter which you use PHP vs Java. But you're on the right track. Most developers create a web service on their HTTP Server and the app talks to that. That's usually in the form of JSON or XML strings over HTTP. Popular choice is using the REST architecture which essentially tells you that stuff you access on the service are resources and you structure your service based on that.
For the PHP vs Java question it's really up to you so do which ever you can setup faster and are more familiar with. I will also say Java has productivity advantages in Android's case because you can create plain old java objects as your models for results and share that code between the server and your Android client. You get this because you can use something like Gson library which serializes and deserializes objects into JSON format. You can also use Google AppEngine for hosting your Java code too.
Well this problem is irrelevant in context to android programming i would say.
Assuming that You are returning the extracted data to your device in json format, the entire performance issue is sort of restricted to the performance of java or php in retrieving data from database and converting it to json and sending to the client.
And as far as simple operations are considered the efficiency wont matter much in both cases, it is just a matter of preference on the developers part.

Android remote MySQL operations using a web service with php

I read some nice articles about how to connect to a remote MySQL database via Android.
Found some really interesting links here and here.
So the common way for getting data seems to be using some kind of webservice (interface, in this case a php script) which queries the db and renders the result in JSON (or XML) format. Then its possible to parse this output with the android JSON_Object implementation. So far so good.
Receiving data from the database and showing it up in a android listview was done in about minutes.
But what is the best practice for writing (inserting) data into tables?
Should a webservice be used here too? (or rather direct mysql conn)
What is the best method to push data to a webservice? (for ex. to insert a new entity in a database) and which format should be used?
In this case I do not use any html forms or anything to post the parameters. So how to post these parameters to the php script? (from within the android app!)
Of course this operation should be secure as well. Implementing a data manipulation machanism is bit more risky (in order to keep the db persistant)
I think, that many apps use some kind of DB, to synchronize data (ex: highscores).
So there should be a best practise for that.
I would recommend keeping anything database-specific hidden behind a web service.
If you build a dependency on MySQL into your application and later find that you need to change databases, the entire installed base has to be cut over. Think about the logistics of accomplishing that for a few minutes and you'll start to realize it's a nightmare.
Premiumsoft's Navicat for MySQL comes with a HTTP tunnel (PHP script) you might be able to use. It basically provides a method for doing anything to a MySQL database over HTTP.
I'd just make sure there are no licensing issues if you plan to distribute your app.

iPhone App: How to get data from an API, parse, and store for later use?

I'm new to iPhone app development so excuse me if I use the wrong terminology, or ask the wrong questions. Basically, I'm trying to write an App that includes downloading a dataset, saving it for later, and checking if the dataset has been updated, but I don't know where to begin.
When I say dataset, I mean a multi-dimensional array of key/value pairs.
I will be creating a site that my App will pull the data from. I think REST is the technology that it will be using (new to REST, too), being served up by a Zend Framework application, using MySQL as the back end database.
So the data will be stored in a MySQL database, and I need to be able to download chunks, which I assume will be stored in my App's SQLite database, to be accessed later (when internet access is not present). At some point in my application's life cycle, I want to check if the dataset that I have downloaded is the most recent version.
There are a lot of parts to this that I am still confused about. Can anyone please shed some light on any/all of the areas that I have touched upon. Is there any iPhone framework that I should be aware of that would make this process go faster/easier?
Thanks!
Update: Maybe I should break this up to make it easier to answer:
I am assuming that my API will retrieve the data from the database and output the response as XML. Is this the best/only option?
In my App, how can I make an API call?
How can I parse the response (probably XML) from an API call and store it for later retrieval?
A really cool technique I found for making web services that will only serve iPhones is to find a plist library for your framework. I usually use Django, and Python has a built-in plist library. This means you can turn your data into a plist object, rather than a standard XML file. This can be serialized and sent over the network just like a standard XML file, but you won't need to use NSXMLParser to parse the file.
From your app, you can make an API call by using the NSData object's method:
+ (id)dataWithContentsOfURL:(NSURL *)aURL
This method will create a data object with the contents of the URL, where the url specified is the address of your api. You can then turn that data object into a string, a dictionary, or whatever your root object in the plist file is.
In order to manage the updating, I usually use a waterlevel method. The iPhone stores the time that it last updated in something simple like NSUserDefaults, and the server stores the time of each entry or update in the database. On the api call, you should pass the server the last updated time as a GET or POST parameter. The server will then search it's database for things updated or entered since then, and send only these objects to the phone.
If just wanting to fetch (GET) data, a REST API call is simply a URL you go to that usually returns XML instead of HTML. Your URL could be www.site.com/rest/fetchstuff or www.site.com/dosomethigncool, doesn't matter. For the app to "call" the API, it'd connect to that URL and include whatever parameters then download the XML from the server.
Let's say your app finds cars for sale. The URL/REST API would be www.site.com/rest/cars/findAll. Your app would call this URL: www.site.com/rest/cars/findAll?make=jeep&model=wrangler. The server would search the database for all cars that are Jeep Wranglers then return the results formatted as XML. Your app would download that XML then parse it.
Check out the SeismicXML sample project. NSURLConnection is what connects to your REST API and downloads the data (XML). NSXMLParser can parse the XML you download.

Categories