Recent Posts

Site Creator

HttpGet Example via Retrofit 2

Posted on

images_m

 

Dear friends

Today we gonna use a very popular library for Making Http requests in Android, Called Retrofit. Its very fast. ( Even faster than Asynctask).

Its lightweight.

Easy to integrate lib. We need to add some dependencies in gradle and we are ready to use it in our application.

But yes we need to do some extra work related to calls but once that is done its turnaround time to bring results is just amazing.
Lets go step by step

we are going to hit the url for fetching data by GET request : http://relsellglobal.in/DeliveryS/CRKInsightsServer/mobilecheck.php?controlVar=32

 

if you click the above link you will find a JSON response will be printed on the screen. So you need to see JSON structure and create your POJO classes accordingly. For eg in the above link

{
“list”: {
“data”: [
{
“ID”: “1”,
“USERNAME”: “Thanks”,
“FCMID”: “eo5uBBIecUI:APA91bFUEnw-fe5Ow4xm3cY8ST2VWs2W4gg8PCG2qX4PqCzlJT1Y6eY5H2sPpUqAQlWyGW3w29VLsVQN7xjwvfeNT6OIEBfLc9gBcja-9Cubl4JnFQDtmYnd2_AZyaVEjz1ZeIKVzin2”,
“EMAILID”: “relsellglobal@gmail.com”,
“userimage”: “http://www.relsellglobal.in/chatapp/profileimages/userprofile.jpg”
}, ……….
] // data closed
} // list closed
} // root closed

So if we refer above , we have a JSON Object at root then we have another JSON Object called list.
There after we have a JSON Array called data . data array is having other JSON Objects. Similarly in Java we will be having a root class ,

Root Object has a WebList Object. WebList object has a data List. data List has WebInnerObject.

WebInnerObject will have properties like ID, EMAILID, userimage, FCMID etc.

 

Please see below files

 

 

 

WebInterfaceService class. This class is required and has methods which are used to interact with HttpServer.

 

Simply our mainactivity is having code that calls the webserviceinterface method.

 

 

And at last but not the lease here is the working code example  Retrofit2BlogDemo

 

Happy Coding !

 

 

 

 







Disclaimer: The aforesaid writeup by Relsell Global writer is for the general understanding of the readers. It does not render any professional advice or opinion.

SHARE THIS PAGE!