Overview

HUAWEI Search Kit fully opens Petal Search capabilities through the device-side SDK and cloud-side APIs, enabling ecosystem partners to quickly provide the optimal mobile app search experience.

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

For details, please refer to Preparations for Integrating HUAWEI HMS Core.
  1. Sign in to AppGallery Connect and select My projects.
  2. Find your project, and go to Project settings > General information > Project information.
  3. Click Set next to Default data processing location, and select a location, which defines the app site, in the displayed dialogue box.
  4. Go to Project settings > Manage APIs, find the Search Kit API, and enable it.
  5. Go to Project settings > General information > App information, and click agconnect-services.json to download the configuration file.
  6. Move the agconnect-services.json file to the app directory (at the same level as src) of your project.
  1. Open the build.gradle file in the root directory of your Android Studio project.
  2. Add the AppGallery Connect plugin and the Maven repository address.
    • Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.
    • Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
    • Go to buildscript > dependencies and add AppGallery Connect plugin configurations.
      buildscript { repositories { google() jcenter() // Configure the Maven repository address of Huawei. maven { url 'https://developer.huawei.com/repo/' } } dependencies { ... // Configure the AppGallery Connect service dependency. classpath 'com.huawei.agconnect:agcp:1.4.1.300' ... } } allprojects { repositories { google() jcenter() // Configure the Maven repository address of Huawei. maven { url 'https://developer.huawei.com/repo/' } } }
  3. Open the build.gradle file in the app directory.
  4. Add build dependencies in the dependencies block.
    dependencies { implementation 'com.huawei.hms:searchkit:5.0.4.303' }
  5. Add the following information under apply plugin: ‘com.android.application' in the file header:
    apply plugin: 'com.huawei.agconnect'
  6. Click Sync Now and wait until synchronization is complete.

Since you have successfully integrated Search Kit, this section will guide you through how to build a demo app to call the API of this Kit for web page search.

  1. Obtain the App ID
    Sign in to AppGallery Connect and select My projects. Find your project, go to Project settings > General information, and obtain the app ID from the App information area.
  2. Obtain the token. For details, please refer to Client Credentials in OAuth 2.0-based Authentication.
  3. Initialize Search Kit in the onCreate method of the activity.
    // Initialize Search Kit. // appID is obtained after your app is created in AppGallery Connect. Its value is of the String type. SearchKitInstance.init(this, appID);
  4. Search for a web page.
    1. Create a request object using WebSearchRequest before a web page search.
      The sample code is as follows:
      WebSearchRequest webSearchRequest = new WebSearchRequest(); // Set the search keyword. (The follow**test**uses test as an example. You can set other keywords as required.) webSearchRequest.setQ("test"); // Set the language for search. webSearchRequest.setLang(Language.ENGLISH); // Set the region for search. webSearchRequest.setSregion(Region.UNITEDKINGDOM); // Set the number of search results returned on a page. webSearchRequest.setPs(10); // Set the page number. webSearchRequest.setPn(1);
    2. Set the token.
      You can use either the setInstanceCredential or setCredential method provided by Search Kit to set a token. The token is used to verify a search request on the server. Search results of the request are returned only after the verification is successful.
      • Use setInstanceCredential to set a global token, which is accessible to all methods.
        The sample code is as follows:
        SearchKitInstance.getInstance().setInstanceCredential(token);
      • Use setCredential to set a token specially for web page search.
        The sample code is as follows:
        SearchKitInstance.getInstance().getWebSearcher().setCredential(token);
    3. (Optional) Set the request timeout interval (in milliseconds).
      You can use setTimeOut to set the timeout interval to a value ranging from 1000 to 10,000. If not specified, Search Kit delivers a default interval of 5000 ms.
      The sample code is as follows:

      SearchKitInstance.getInstance().getWebSearcher().setTimeOut(5000);
    4. Start a web page search.
      Pass webSearchRequest constructed in 1 as a parameter to the search method.
      The sample code is as follows:

      BaseSearchResponse<List<WebItem>> response = SearchKitInstance.getInstance().getWebSearcher().search(webSearchRequest);
    1. Start your demo app. Different APIs will be called when you perform a search on different tab pages.
    2. Enter a keyword in the search box. The auto suggestion API is called in real time.
    3. Tap the search icon. The search results are displayed on the corresponding tab page. The data returned by the spelling check API is also displayed under the search box.

    4. Tap the title of a search result. You will be redirected to the destination page based on the value of click_url.
    5. Tap Detail >> to go to the details page and view all the parameters returned through the API.
    6. Tap the plus sign (+) to view more return values in an object.

    Well done. You have successfully completed this codelab and learned how to:

    • Integrate Search Kit.
    • Call the Search Kit API for web page search.

    For more information, please click the following link:

    Related documents
    To download the source code, please click the following link:

    Download source code

    已复制代码