Skip to main content

RESTFul Client on Android

I was surfing on net , i didn't get a needed help on RESTful Client on Android, so I decided to write my own.
Following is the code..

First of all REST service architecture uses all the basic operations of HTTP to interact.

First of all u would have to set make a HttpGet method with the URI to be requested.

HttpGet httpGet=new HttpGet(new URI("www://abc.con");

i have used new URI() as a get parameter because if u are sending some parameters with the GET request , it will encode the parameters ready to be sent along the network channel.

Second step is that u will have to make a HttpClient which will execute the GET request that we have made.
It is made as follows--
HttpClient httpClient=new DefaultHttpClient();

now execute the GET request from the HttpClient
as
Make a variable to hold Response of the request
HttpResponse httpResponse=httpClient.execute(httpGET);

to convert the Response to string use the following line
String result=EntityUtils.toString(httpResponse.getEntity());


So ur result is ready as a string, u can use various parser according to response types ie XML and JSON.


In the next post i will explain the basics of Rest Client by using Spring Android Framework , which is better and easy to use, u do not have to consider any response types ,it automatically parses all data for u,
So wait for next article.

Comments

  1. I forgot to mention in this post that , all netwotrk related operations , ie
    client.execute() where response is feteched,infact u should run all the above code using AsyncTask or using Thread, because ANdorid does not allow network related operations to be running on main UI thread , it will throw out NetworkOnMainUIThreadException .

    ReplyDelete

Post a Comment

Popular posts from this blog

Database Sharding

Collating some of the resources which talks about Database Sharding. https://en.wikipedia.org/wiki/Shard_(database_architecture) [Feb 2019]  http://highscalability.com/blog/2019/2/19/intro-to-redis-cluster-sharding-advantages-limitations-deplo.html Redis Cluster is the Native Sharding implementation available within Redis that allows your to automatically distribute your data across multiple nodes without having to rely on external tools and utilities. Its covers Sharding with Redis Cluster  where Redis Clusters is divided in 16384 slots and these slots are assigned to multiple Redis Nodes. The  Redis Cluster Specification  is the definitive guide to understanding the internals of the technology, while the  Redis Cluster Tutorial  provides deployment and administration guidelines. [ Jan 2019  ]  https://scalegrid.io/blog/scalegrid-hosting-adds-support-for-highly-available-redis-clusters-with-automated-sharding/ ScaleGrid : Fully Manage...
Surrounded by Idiots Do you sometimes feel like you’re surrounded by idiots who just don’t understand anything you say? Odds are that they’re not actually idiots, but their personality types are just different than yours. Know the types of people around you As far back as the fifth century BC, the Greek physician Hippocrates identified four main personality types, which author Thomas Erikson denotes with specific colors. The Red, or Dominant, personality is characterized by ambition, dominance, decisiveness, competitiveness, and an often hot temper. The Yellow, or Inspiring, personality is optimistic, cheerful, and confident. When the game seems lost to everyone else, Yellows will continue finding opportunities. The third personality type is Green, or Stable, denoted by a calm, balanced, easygoing, tolerant, and friendly nature. Greens are great team players, because the last thing they want is to cause a fuss or conflict of any kind. Finally, there’s the Blue, or ...