Written by

Hari Murugesan

Share

  

Category

see all categories
intranet as a service

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\"",

            "odata.editLink": "Web/Lists(guid'f2f42ab3-a7be-4fb3-992b-e7318d1381af')/Items(1)",

            "FileSystemObjectType": 0,

            "Id": 1,

            "ServerRedirectedEmbedUri": null,

            "ServerRedirectedEmbedUrl": "",

            "ContentTypeId":

"0x010043B6F464D7D7BC4DBF12CD23AD337DDC006A3A25FB736CC34D99FEC12584EAF552",

            "Title": "cricket tournament",

            "Location": "Coimbatore",

            "Location": "Coimbatore",

            "EventDate": "2017-10-05T13:53:00Z",

            "EndDate": "2017-10-06T13:53:00Z",

            "Description": "Cricket match",

            "fAllDayEvent": false,

            "fRecurrence": false,

            "ParticipantsPickerId": null,

            "ParticipantsPickerStringId": null,

            "Category": "(2) Category2",

            "FreeBusy": null,

            "Overbook": null,

            "BannerUrl": null,

            }]

}

 

If you have followed me from the beginning, you would have been able to get your SharePoint list data to your Android app. Feel free to download the code package and deploy for yourself. If you are struck anywhere or need any support, don’t hesitate to ask me in the comments column.

Hari Murugesan

Written by Hari Murugesan

Everybody gets crazy with iPhone release dates, but not Hari. He is so obsessed with Android and obviously, the perfect person to handle our Android apps development. When he plugs his earphone on and clicks opens Android Studio, we can trust he started shaping the App he is on. A gym bunny who spends the rest of the time shaping him as well.