Categories:

How to get SharePoint list item in Android using REST API

Welcome readers again to a blog related to SharePoint in Android. Hope you enjoyed my previous blog on How to display images from SharePoint on your Android. In today’s post, let me take you through how to get your SharePoint list item in Android using REST API.

I have shared you the code zip file so that you can download it straight away and implement for your needs.

SharePoint list item in Android

Open Android Studio -> Create a New Project.

Now you need to login to SharePoint via Android. Here is a detailed article on how to Login to SharePoint via Android.

Once you have got “RTFA” and “Fed_Auth” access, that means you are logged in to SharePoint. Now, it is time to create a RestApi call.

To do that, you need to add the three libraries viz., httpclient, httpcore, httpmime to the Library folder

Create a new class to initialize. Find below the DefaultHttpClient code.

               DefaultHttpClient httpClient = new DefaultHttpClient(); 

Then you need to initialize the Httppost and your posting URL is send through HttpPost. Below is the code for that.

   HttpGet httpGet = new HttpGet(

   â€œYour  tenant name”+“/_api/web/Lists/GetByTitle(‘your List Name’)/Items”);

Then you need to initialize the Httppost and your posting URL is send through HttpPost. Below is the code for that.

   HttpGet httpGet = new HttpGet(

 â€œYour  tenant name”+“/_api/web/Lists/GetByTitle(‘your List Name’)/Items”);

 Once you are done with that, add RTFA and FedAuth in your header file. Also, you need to add “Content_type” and “Accept” for which the code is given below.

httpGet.setHeader(“Cookie”, “rtFa=” + mHashMap.get(“auth_id”) + “; FedAuth=” + mHashMap.get(“auth_token”));
httpGet.setHeader(“Content-type”, “application/json;odata=verbose”);
httpGet.setHeader(“Accept”, “application/json;odata=verbose”);

Now, initialize one more protocol namely, HttpResponse. The same has been executed in the code as shown below.

HttpResponse httpResponse = httpClient.execute(httpGet);

Now that your code is executed, you need to check on the response code using  mHttpResponse for which the code is given below.

               int postStatusCode = httpResponse.getStatusLine().getStatusCode();

Once your statusCode returns 200, that means you get a SharePoint list.

Now, you need to get a response via HttpEntity and then need to read your list data using BufferedReader and StringBuilder. The code that performs these is shown below.

BufferedReader reader = new BufferedReader(new InputStreamReader(is, “iso-8859-1”), 8);

String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line + n);
}

Utility.logging(“Sb String” + sb.toString());

Now you got response value which you can convert into JSONObject and parse it. The code for same is below

JSONObject mJsonObject = new JSONObject(sb.toString());

SharePoint list sample response:

{

    “odata.metadata”:

 “https://hubflysoft.SharePoint.com/sites/QA/_api/$metadata#SP.ListData.EventsHFListItems”,

    “value”: [   

         {

            “odata.type”: “SP.Data.EventsHFListItem”,

            “odata.id”: “f0bac20c-abaf-40bb-bc3e-f76f9598a3d9”,

            “odata.etag”: “”1″”””

Sharing is Caring!

Leave a Reply

Your email address will not be published. Required fields are marked *