Search code examples
javaandroidresthttprequestonedrive

How to send a search request using OneDrive SDK for Android


I am desperately trying to understand how to use the OneDrive SDK for Android. The sample apps only describe picking, saving or exploring. I have implemented the explorer available here: https://github.com/OneDrive/onedrive-explorer-android

and in the end, I have this kind of code:

final IOneDriveService oneDriveService = oneDriveHelper.getOneDriveService();
final Callback<Item> itemCallback = getItemCallback(app);
oneDriveService.getItemId(mItemId, mQueryOptions, itemCallback);

where

mItemId="root";

I tried changing the mQueryOptions by doing this

mQueryOptions.put("q", "myKeyWord");

without success (it's just listing the root)

I tried replacing mItemId with:

"root:/view.search"

without any more success.

http://onedrive.github.io/items/search.htm


Solution

  • Ok, I finally understood how it all worked.

    So since the ApiExplorer is just a sample, we need to add more functionalities by hand into it.

    In the Module called onedriveaccess, go to the package com.microsoft.onedriveaccess.model and add the following file ItemList.java

    package com.microsoft.onedriveaccess.model;
    
    import com.google.gson.annotations.SerializedName;
    import java.util.List;
    
    public class ItemList {
        @SerializedName("value")
        public List<Item> itemList;
    }
    

    Then in com.microsoft.onedriveaccess.IOneDriveService.java, add the following piece of code:

    @GET("/v1.0/drive/{item-id}/view.search")
    @Headers("Accept: application/json")
    void searchForItemId(@Path("item-id") final String itemId,
            @QueryMap    Map<String, String> options,
            final Callback<ItemList> itemCallback);
    

    now we can generate a search query as follows:

      /**
     * The query option to have the OneDrive service expand out results of navigation properties
     */
    private static final String EXPAND_QUERY_OPTION_NAME = "expand";
    
    /**
     * Expansion options to get all children, thumbnails of children, and thumbnails
     */
    private static final String EXPAND_OPTIONS_FOR_CHILDREN_AND_THUMBNAILS = "children(select=id, name)";
    
    
    private final Map<String, String> mQueryOptions = new HashMap<>();
    
    
    private Callback<ItemList> getItemsCallback(final Context context) {
        return new OneDriveDefaultCallback<ItemList>(context) {
            @Override
            public void success(final ItemList items, final Response response) {
                //mItem = items.itemList.get(0);
    
                //Do what you want to do
    
                for(Item item: items.itemList){
                  Log.v(TAG, "array:"+item.Id+"--- "+item.Name);
                }
            }
    
            @Override
            public void failure(final RetrofitError error) {
    
                //Log.v(TAG, "Item Lookup Error: " + mItemId);
    
            }
        };
    }
    
    
    public void searchQuery(String query){
        mQueryOptions.put(EXPAND_QUERY_OPTION_NAME, EXPAND_OPTIONS_FOR_CHILDREN_AND_THUMBNAILS);
        mQueryOptions.put("q", query);
    
        final IOneDriveService oneDriveService = oneDriveHelper.getOneDriveService();
        final Callback<ItemList> itemCallback = getItemsCallback(app);
        oneDriveService.searchForItemId(mItemId, mQueryOptions, itemCallback);
    
    }