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.
I forgot to mention in this post that , all netwotrk related operations , ie
ReplyDeleteclient.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 .