【问题标题】:Use regular search Google api with Retrofit使用常规搜索 Google api 和 Retrofit
【发布时间】:2020-03-13 08:07:50
【问题描述】:

我有 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 = "something")

但应用程序崩溃了

.baseUrl(BASE_URL)

任何帮助将不胜感激!

【问题讨论】:

  • 你能发布你的错误日志吗
  • @Deepak java.lang.IllegalStateException:无法执行方法。在 com.example.googlesearchapp.api.ApiFactory.(ApiFactory.kt:12)

标签: android kotlin google-api retrofit google-search-api


【解决方案1】:

我已经在我的一个项目中成功实现了 Google Places Search API。下面是实现代码:

APISERVICE:- @GET("api/place/nearbysearch/json?&key=GOOGLE_PLACE_API_KEY") 调用 getNearbyCall(@Query("YOUR_QUERY_TYPE") 字符串类型,@Query("LATITUDE_LONGITUDE") 字符串位置,@Query("RADIUS_IN_METER") int radius);

改造客户:-

    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));
            }
        });

    }

【讨论】:

  • 我询问过 Google 的常规搜索
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2016-05-09
  • 2013-01-14
  • 1970-01-01
相关资源
最近更新 更多