Android 改进动态URL问题
我是翻新2的新手,正在尝试在我的应用程序中集成GooglePlaceAPI。我的问题是,如何在使用改型2.0时使用这种动态URL 网址: 我的模型类名称是: 1.自动完成 2.地点预测Android 改进动态URL问题,android,retrofit2,Android,Retrofit2,我是翻新2的新手,正在尝试在我的应用程序中集成GooglePlaceAPI。我的问题是,如何在使用改型2.0时使用这种动态URL 网址: 我的模型类名称是: 1.自动完成 2.地点预测 public class PlaceAutoComplete { private String place_id; private String description; public String getPlaceDesc() { return description; } public void
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;
}
}
及
我在调用URL时使用了这个接口:但是我无法继续使用这个接口
public interface RetrofitService {
@GET("?input=")
Call<PlaceAutoComplete> getInput(@Url String url);
}
我一直在谷歌和StackOverflow中搜索,但没有让我明白。详细的解释将非常有价值
谢谢。改装1:
你为什么要这么做?您可以在这里找到:在您的界面中
@得到
呼叫loadProfile@Url字符串url
从调用服务器的位置添加此函数
public void loadServerData() {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<PojoClass> call = apiInterface.loadProfile("https://maps.googleapis.com/maps/api/place/autocomplete/json?input="{Place Name}"&location="{Lat,long}"&key="{API KEY}"");
call.enqueue(new Callback<PojoClass>() {
@Override
public void onResponse(Call<PojoClass> call, Response<PojoClass> response) {
updateUI();
}
@Override
public void onFailure(Call<PojoClass> call, Throwable t) {
}
});
}
和APIClient类
public class ApiClient {
private static final String Base_URL = Constants.URL.BASE_URL;
private static Retrofit retrofit = null;
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create());
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()))
.build();
}
return retrofit;
}
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null);
}
public static <S> S createService(Class<S> serviceClass, final RequestBody body) {
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Accept", "application/json")
.method(original.method(), body);
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}
}只有模型类名有什么帮助?发布他们的代码too@VivekMishra我发布模型类中包含的代码..引用此http://stackoverflow.com/questions/32559333/retrofit-2-dynamic-url@PushpendraChoudhary老兄,你确定你的代码是改装2.x版的,你能检查一下你的梯度吗。因为okHttp,AFAIK语法对于改型2.x是不同的。哦,对不起,这是针对改型1的。我将为改造2进行更新。@PushpendraChoudhary此语法适用于改造1.x,也适用于FormUrlEncoded与POSTrequest@PushpendraChoudhary此外,基本url应以/@Kaushik结尾。感谢提醒:已适当更新答案。
public interface RetrofitService {
@GET("?input=")
Call<PlaceAutoComplete> getInput(@Url String url);
@GET("/place/autocomplete/json")
void getDetails(
@Query("input") String input,
@Query("location") String location,
@Query("key") String key,
Callback<Response> callback);
@GET("/place/autocomplete/json")
@FormUrlEncoded
void getDetails(
@FieldMap Map<String, String> params,
Callback<Response> callback);
@GET("place/autocomplete/json")
Call<List<Response>> getDetails(
@Query("input") String input,
@Query("location") String location,
@Query("key") String key);
@GET("place/autocomplete/json")
Call<List<Response>> getDetails(
@QueryMap Map<String, String> options);
retrofitService = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/")
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
public void loadServerData() {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<PojoClass> call = apiInterface.loadProfile("https://maps.googleapis.com/maps/api/place/autocomplete/json?input="{Place Name}"&location="{Lat,long}"&key="{API KEY}"");
call.enqueue(new Callback<PojoClass>() {
@Override
public void onResponse(Call<PojoClass> call, Response<PojoClass> response) {
updateUI();
}
@Override
public void onFailure(Call<PojoClass> call, Throwable t) {
}
});
}
public class ApiClient {
private static final String Base_URL = Constants.URL.BASE_URL;
private static Retrofit retrofit = null;
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create());
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()))
.build();
}
return retrofit;
}
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null);
}
public static <S> S createService(Class<S> serviceClass, final RequestBody body) {
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.header("Accept", "application/json")
.method(original.method(), body);
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}