簡體   English   中英

如何使用翻新創建請求URL?

[英]How to create request URL using Retrofit?

我想為此API創建GET請求: http://maps.googleapis.com/maps/api/geocode/json?latlng=myLatitude,myLongitude&sensor=true : http://maps.googleapis.com/maps/api/geocode/json?latlng=myLatitude,myLongitude&sensor=true

代替myLatitudemyLongitude ,我需要輸入自己的值。 在下面,您可以看到我如何嘗試使用Retrofit進行請求:

Api接口:

@GET("json?latlng={lat},{long}&sensor=true")
Call<JsonArray> getApproximateLocations(@Query(value = "lat" , encoded = true) String lat, @Query(value = "long" , encoded = true) String lon);

我的請求:

Retrofit retrofit = new Retrofit.Builder().baseUrl(getString(R.string.location_base_api)).addConverterFactory(GsonConverterFactory.create())
            .build();


    ApiInterface apiInterface = retrofit.create(ApiInterface.class) ;

    mapsApiInterface.getApproximateLocations(String.valueOf(lat),String.valueOf(lon)).enqueue(new Callback<JsonArray>() {
        @Override
        public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {

        }

        @Override
        public void onFailure(Call<JsonArray> call, Throwable throwable) {

        }
    });

我的基本網址是: http://maps.googleapis.com/maps/api/geocode/ : http://maps.googleapis.com/maps/api/geocode/

該代碼的結果是以下錯誤: java.lang.IllegalArgumentException: URL query string "latlng={lat},{long}&sensor=true" must not have replace block. For dynamic query parameters use @Query. java.lang.IllegalArgumentException: URL query string "latlng={lat},{long}&sensor=true" must not have replace block. For dynamic query parameters use @Query.

那么,為什么我會收到此錯誤以及如何解決該問題?

這樣做:

@GET( Constants.GET_ADDRESS_BY_LOCATION )
    ServiceCall<Response> getAddress(@Query(Constants.PATH_LATLNG) String latlng, @Query(Constants.PATH_LANGUAGE) String language , @Query(Constants.KEY_GOOGLE_MAP_API) String key);

GET_ADDRESS_BY_LOCATION是API,而其他常量是必需參數

@GET("json?latlng={lat},{long}&sensor=true")
Call<JsonArray> getApproximateLocations(@Query(value = "lat" , encoded = true) String lat, @Query(value = "long" , encoded = true) String lon);

應該是這樣的:

@GET("json")
Call<JsonArray> getApproximateLocations(
    @Query(value = "latlng" , encoded = true) String latLng/* pass formatted string here (e.g. 2.1367,2.3199 */, 
    @Query(value = "sensor") boolean isSensor);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM