Saturday, July 4, 2015

Retrieve Data from Server and Local

Update : 


For new version of RESTManager library, please refer on this github page,
Github | RESTLibrary-Android--BETA--



It been long time since last post...

This time I will share code that I use to handle data during development and integration.



Above picture is the app I created for this post, before that let's start from basic,

In order to connect the device with server(or backend), we need a Network Connection.
In android, to use device functionality, we need declare/ask the permission for a function.
For this case, we need Internet Connection from device, then need include this permission in AndroidManifest.xml, before <application> tag



and to receive data from server before API 22, we can use  HttpPost or HttpGet,

HttpPost Example


HttpGet Example



But for API 22, this method already deprecated and need use HttpURLConnection or HttpsURLConnection (added since API 1).

HttpURLConnection


HttpsURLConnection



For POST method usually request data send in JSON as raw input, and
For GET method usually pair value attach with URL.

The response receive in plain string and the common response's data type is JSON.

In order to parse the plain string to JSON format, we can use JSONObject and JSONArray which provided in Android library, or

Use GSON Library (I used in my code), which parse the plain string to a model/object (save lot of time and energy).

So, All of the above explain about receive data from server. 

For local, the temporary/dummy data will store in Asset Folder.



To read the file just call context.getAssets().open(FILENAME) .

ReadAsset



Here the library and sample app,

Library download
Sample Project download
Server-side download



Sample Project Explanation :

**This project develop using Android Studio 3.0, all java code below wrote in MainActivity,java

1) to use RESTManager library, need include GSON as dependencies on project,



2) Create a class that listing all path for local and back-end by using Path.java.



3) Initialize configuration for RESTManager,



4) Below the sample code how to use POST method in RESTManager (without request parameter).



5) Below the sample code how to use POST and GET method in RESTManager (with request parameter).





6) Using WAMP, XAMPP or MAMP as server(localhost), extract rest.zip in,

WAMP "wamp/www/"
MAMP"mamp/htdoc/"
XAMPP"xampp/htdoc/"

7) Connect phone and computer with same WI-FI connection, or using Wireless Tethering/Hotspot.

8) Retrieve IP address using command prompt or terminal, this IP address will use it later






5) Here the sample running, set IP address you retrieve before, then you can play with it,

  • username : admin
  • password : 1234












If you have problem or something to ask that related to java...
you can email or comment on this post...
I will try to find the solution and share it with you..












you can test it by your self..
i hope you like this souce code...
and please give some comment....
                                                                                                                                                      Created By : Z-man, 2015

No comments:

Post a Comment