Ram*_*asu -2 android retrofit2
我是Retrofit 2的新手,我正在尝试在我的应用中集成Google Place API.我的问题是如何在使用Retrofit 2.0时继续使用这种动态URL.
网址:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input="{Place Name}"&location="{Lat,long}"&key="{API KEY}"
Run Code Online (Sandbox Code Playgroud)
我的模型类名称是:
1)PlaceAutoComplete
2)PlacePredictions
public class PlaceAutoComplete {
private String place_id;
private String description;
public String getPlaceDesc() {
return description;
}
public void setPlaceDesc(String placeDesc) {
description = placeDesc;
}
public String getPlaceID() {
return place_id;
}
public void setPlaceID(String placeID) {
place_id = placeID;
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class PlacePredictions {
public ArrayList<PlaceAutoComplete> getPlaces() {
return predictions;
}
public void setPlaces(ArrayList<PlaceAutoComplete> places) {
this.predictions = places;
}
private ArrayList<PlaceAutoComplete> predictions;
}
Run Code Online (Sandbox Code Playgroud)
我已经为Retrofit创建了WebServiceCall.java类,这是我的代码
public class WebServiceCall {
private static WebServiceCall webServiceCall;
public RetrofitService retrofitService;
private String currentDateTimeString;
public WebServiceCall() {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
if (Boolean.parseBoolean("true")) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.connectTimeout(100000, TimeUnit.MILLISECONDS);
clientBuilder.addInterceptor(httpLoggingInterceptor);
}
retrofitService = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/place/autocomplete/json")
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(RetrofitService.class);
currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
}
public static WebServiceCall getInstance() {
if (webServiceCall == null) {
webServiceCall = new WebServiceCall();
}
return webServiceCall;
}
}
Run Code Online (Sandbox Code Playgroud)
我在调用URL时使用此接口:但我无法继续前进.
public interface RetrofitService {
@GET("?input=")
Call<PlaceAutoComplete> getInput(@Url String url);
Run Code Online (Sandbox Code Playgroud)
}
我一直在谷歌和StackOverflow搜索,但没有让我理解.详细解释将非常值得注意.
谢谢.
改造1:
@GET("/place/autocomplete/json")
void getDetails(
@Query("input") String input,
@Query("location") String location,
@Query("key") String key,
Callback<Response> callback);
Run Code Online (Sandbox Code Playgroud)
如果参数未知,您应该创建如下参数:
@GET("/place/autocomplete/json")
@FormUrlEncoded
void getDetails(
@FieldMap Map<String, String> params,
Callback<Response> callback);
Run Code Online (Sandbox Code Playgroud)
改造2
@GET("place/autocomplete/json")
Call<List<Response>> getDetails(
@Query("input") String input,
@Query("location") String location,
@Query("key") String key);
Run Code Online (Sandbox Code Playgroud)
对于未知的参数:
@GET("place/autocomplete/json")
Call<List<Response>> getDetails(
@QueryMap Map<String, String> options);
Run Code Online (Sandbox Code Playgroud)
并且您应该使用尾随/在基本URL的末尾进行这样的设置:
retrofitService = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/")
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
Run Code Online (Sandbox Code Playgroud)
为什么要这样做?你可以在这里找到:https://stackoverflow.com/a/32356916/3863689
| 归档时间: |
|
| 查看次数: |
2439 次 |
| 最近记录: |