Hi Friends,
About This Blog
I am writing this blog to show you
how to implement the REST API in your android app.I am using my Local Apache
Server and MySQL database to perform this task. Here I am mentioning the step
which I have followed to implement it:
- Connect my app to my Local Apache Server and send some data to it.
- My Apache server with the help of servlet gets connected to the MySQL database and store above data into the Database.
- App request for the stored data in the MySQL database by again connecting to the Local Apache Server.
- Now at Server a servlet is created which fetch data from MySQL database and provide it to the app in the form JSON data.
- Now the app parses this JSON data and show it to the user in the form of List.
Requirements
·
IDE: Eclipse
·
Android
SDk: sdk4.0.3 Ice Cream Sandwich
·
Server:
Local Apache Server
·
Database:
MySQL database
Detailed Steps
Step 1:
As in step 1 I have to connect my
app to the local apache server, to perform this task I am using AsyncTask. As for connecting app to the
local server not require User Interface hence I am using AsyncTack abstract
class. With the help of this class doInBackground(Params...) method we can connect app to
server in the background
Without
disturbing User Interface.
To perform the actual connecting in the
doInBackground() method I am using :
·
HttpClient
interface to execute HTTP requests while handling
cookies,authentication,connection management, and other features.
·
HttpContext
class object is used to tie together the request, the response, and optional
application data.
·
HttpGet class which retrieve whatever information
from URL (in the form of an entity) is identified by the Request-URI.
Now whatever is the url given to the HttpGet class
constructor ,app get connected to that address. We are also passing the data
with above url to store into local database.
Step 2:
When server receives the request from the android app
it firstly fetches the data from the url using HttpServletRequest class
getParameter() method. This server create an connection to the local MySQL
database and store this retrieve data into it.
Step 3:
In above doInBackground() method I have receive
response from the servlet with the help HttpEntity class method getEntity().
Step 4:
Now server gives response to the android app by
firstly fetching the data from the
MySQL database then represent that data in the form of
JSON with the help of JSONObject and JSONArray.
Step 5:
Now the JSON data received by the android app first
get parsed and then store into
the array. You
can done anything with this data received and used it according to your
requirements.
Practical Example
Hello Friends please don’t get bored now
I am going to show you how to implement the REST API in reality.Here in this
app user enter the Name and Age,when user click on the
Submit button then the entered data get
inserted into the database.When user click on ViewData then the entered data is
show to the user in the form of ListView.
Step1:
I
have used:
Application
Name: “Connect App To Apache Mysql”:
Package
Name: ”com.connect.androidapp.to.apache.mysql”
Target
SDK: Android 4.0.3(API 15)
for source code contact me here: