改进动态URL问题

时间:2017-01-03 06:51:41

标签: android retrofit2

我是Retrofit 2的新手,我正在尝试在我的应用中集成Google Place API。我的问题是如何在使用Retrofit 2.0时继续使用这种动态URL。

URL:

  https://maps.googleapis.com/maps/api/place/autocomplete/json?input="{Place Name}"&location="{Lat,long}"&key="{API KEY}"

我的模型类名称是:

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

}

public class PlacePredictions {

  public ArrayList<PlaceAutoComplete> getPlaces() {
    return predictions;
}

  public void setPlaces(ArrayList<PlaceAutoComplete> places) {
    this.predictions = places;
}

  private ArrayList<PlaceAutoComplete> predictions;
}

我已经为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;
}
}  

我正在使用此界面调用URL:但我无法继续前进。

 public interface RetrofitService {

@GET("?input=")
Call<PlaceAutoComplete> getInput(@Url String url);

}

我一直在google和StackOverflow中搜索,但没有让我理解。详细解释将非常值得注意。

谢谢。

2 个答案:

答案 0 :(得分:5)

改造1:

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

改造2

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

并且您应该使用尾随/在基本URL的末尾进行这样的设置:

retrofitService = new Retrofit.Builder() 
            .baseUrl("https://maps.googleapis.com/maps/api/")
            .client(clientBuilder.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build()

为什么你应该这样做?你可以在这里找到:https://stackoverflow.com/a/32356916/3863689

答案 1 :(得分:0)

在您的界面

  

@GET       调用loadProfile(@Url String 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 Class

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

}