Используйте регулярный поиск Google API с Retrofit - PullRequest
0 голосов
/ 13 марта 2020

У меня есть ApiService

interface ApiService {
    @GET("customsearch/v1")
    fun getTopResult(
        @Query(QUERY_PARAM_API_KEY) key: String = "AIzaSyFhdJHf....7IlRcE",
        @Query(QUERY_PARAM_CX) cx: String = "017576662512468239146:omuauf_lfve",
        @Query(QUERY_PARAM_QUERY) q: String
    ) : Single<ResultInfoList>

    companion object {
        private const val QUERY_PARAM_API_KEY = "key"
        private const val QUERY_PARAM_CX = "cx"
        private const val QUERY_PARAM_QUERY = "q"
    }

}

ApiFactory:

object ApiFactory {

    private const val BASE_URL = "https://www.googleapis.com/"

    private val retrofit = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build()

    val apiService = retrofit.create(ApiService::class.java)

}

Я пытаюсь получить ответ Json, вызывая ApiFactory.apiService.getTopResult (q = "что-то")

но приложение рушится на линии

.baseUrl (BASE_URL)

Любая помощь будет оценена по достоинству!

1 Ответ

0 голосов
/ 14 марта 2020

Я успешно внедрил API поиска Google Адресов в одном из моих проектов. Ниже приведен код реализации:

APISERVICE: - @GET ("api / place / nearsearch / json? & Key = GOOGLE_PLACE_API_KEY") Вызовите getNearbyCall (@Query ("YOUR_QUERY_TYPE") ) Тип строки, @Query ("LATITUDE_LONGITUDE") Расположение строки, @Query ("RADIUS_IN_METER") int radius);

RETROFITCLIENT: -

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl("https://maps.googleapis.com/maps/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;

И мой последний вызов API: -

private void doRequestForGooglePlaceApi(double latitude, double longitude, int locationRadius, String locationType) {
        APIService mApiService = RetrofitClient.getClient("https://maps.googleapis.com/maps/").create(APIService.class);

        Call<NearbyModel> call = mApiService.getNearbyCall(locationType, latitude + "," + longitude, locationRadius);

        call.enqueue(new Callback<NearbyModel>() {
            @Override
            public void onResponse(Call<NearbyModel> call, Response<NearbyModel> response) {
                if (response.isSuccessful() && response.body() != null && response.body().getResults() != null) {
                    setNearbyAdapter(response.body().getResults());
                } else {
                    Globals.showToast(NearbyActivity.this, getString(R.string.msg_something_went_wrong));
                }
            }

            @Override
            public void onFailure(Call<NearbyModel> call, Throwable t) {
                Globals.showToast(NearbyActivity.this, getString(R.string.msg_something_went_wrong));
            }
        });

    }
...