Introduction

With HUAWEI Site Kit, your app can provide users with convenient and secure access to diverse, place-related services. HUAWEI Site Kit provides the following core capabilities you need to quickly build apps with which your users can explore the world around them:

What You Will Create

In this codelab, you will create a place-based app, for example:

What You Will Learn

Hardware Requirements

Software Requirements

To integrate HUAWEI HMS Core services, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

1.Open Android Studio, go to File > New > New Project, and select Empty Activity to create a project.
2.Configure the Maven repository address for the HMS SDK.

3.Add build dependencies.

1.Add EditText to the layout to enter search keywords, add TextView to display search results, and add other components as required.

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="bottom" android:background="#D3D3D3" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="Parameters" android:textSize="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Query: " /> <EditText android:id="@+id/edit_text_text_search_query" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:autofillHints="" android:hint="" android:imeOptions="actionGo" android:inputType="text" android:text="Eiffel Tower" /> </LinearLayout> <Button android:id="@+id/button_text_search" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="search" android:text="Search" android:textAllCaps="false" /> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="bottom" android:background="#D3D3D3" android:gravity="center_vertical" android:paddingLeft="5dp" android:text="Result" android:textSize="16sp" /> <TextView android:id="@+id/response_text_search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" /> </LinearLayout> </ScrollView>

2.Declare a SearchService object and use the SearchServiceFactory class to instantiate the object in the onCreate method.

package com.huawei.codelab.sitecodelab; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import com.huawei.hms.site.api.SearchResultListener; import com.huawei.hms.site.api.SearchService; import com.huawei.hms.site.api.SearchServiceFactory; import com.huawei.hms.site.api.model.AddressDetail; import com.huawei.hms.site.api.model.SearchStatus; import com.huawei.hms.site.api.model.Site; import com.huawei.hms.site.api.model.TextSearchRequest; import com.huawei.hms.site.api.model.TextSearchResponse; import androidx.appcompat.app.AppCompatActivity; /** * site activity entrance class * * @author David * @since 2019-12-12 */ public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private SearchService searchService; TextView resultTextView; EditText queryInput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchService = SearchServiceFactory.create(this); queryInput = findViewById(R.id.edit_text_text_search_query); resultTextView = findViewById(R.id.response_text_search); } }

3.Create a TextSearchRequest object, which will be used as the place search request body. Related parameters are as follows, among which query is mandatory and others are optional:

4.Call the textSearch API and pass the TextSearchRequest object created in step 3 and the SearchResultListener object. Then obtain search results from the onSearchResult method and display them.

public void search(View view) { TextSearchRequest textSearchRequest = new TextSearchRequest(); textSearchRequest.setQuery(queryInput.getText().toString()); searchService.textSearch(textSearchRequest, new SearchResultListener<TextSearchResponse>() { @Override public void onSearchResult(TextSearchResponse textSearchResponse) { StringBuilder response = new StringBuilder("\n"); response.append("success\n"); int count = 1; AddressDetail addressDetail; for (Site site : textSearchResponse.getSites()) { addressDetail = site.getAddress(); response.append(String.format( "[%s] name: %s, formatAddress: %s, country: %s, countryCode: %s \r\n", "" + (count++), site.getName(), site.getFormatAddress(), (addressDetail == null ? "" : addressDetail.getCountry()), (addressDetail == null ? "" : addressDetail.getCountryCode()))); } Log.d(TAG, "search result is : " + response); resultTextView.setText(response.toString()); } @Override public void onSearchError(SearchStatus searchStatus) { Log.e(TAG, "onSearchError is: " + searchStatus.getErrorCode()); } }); }

Building, Loading, and Debugging the App

Open Gradle built in Android Studio and double-click installDebug to install the developed app.

Preparing for the Debugging

Open the demo app, click Search to search for places based on the preset search keyword Eiffel Tower, and check whether the search results can be properly displayed.

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

For more information, please refer to HUAWEI Site Kit Developer Guide.
Download the sample code used in this codelab from the following address:

You can download the source code in github.com/Huawei/Consumer/tree/master/Codelabs/SiteKit

You can also click the button below to download the source code.

Download source code

Code copied