-3

I am new with Retrofit 2, and am trying to integrate a Google Place API in my App. My Question is how to move forward with this kind of Dynamic URL while using Retrofit 2.0.

URL:

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

My Model Classes Name are:

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

}

AND

public class PlacePredictions {

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

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

  private ArrayList<PlaceAutoComplete> predictions;
}

And I have create the WebServiceCall.java class for Retrofit, This is my Code

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

And I am using this Interface in Call the URL: but I unable to move forward with this.

 public interface RetrofitService {

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

}

I been search in the google and StackOverflow, but not make me understand. A detailed explanation will be highly appreciable.

Thank you.

Ramanuj Basu
  • 77
  • 3
  • 15

2 Answers2

6

Retrofit 1:

@GET("/place/autocomplete/json")
void getDetails(
             @Query("input") String input,
             @Query("location") String location, 
             @Query("key") String key,
             Callback<Response> callback);

If parameters are unknown, you should create parameter like this:

@GET("/place/autocomplete/json")
@FormUrlEncoded
void getDetails(
              @FieldMap Map<String, String> params,
              Callback<Response> callback);

Retrofit 2

    @GET("place/autocomplete/json")
    Call<List<Response>> getDetails(
                            @Query("input") String input,
                            @Query("location") String location, 
                            @Query("key") String key);

for unknown params:

  @GET("place/autocomplete/json")
  Call<List<Response>> getDetails(
                             @QueryMap Map<String, String> options);

and you should do your setup like this with trailing / in the end of base URL:

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

Why you should do that? you can find out here: https://stackoverflow.com/a/32356916/3863689

Community
  • 1
  • 1
Pushpendra
  • 2,791
  • 4
  • 26
  • 49
0

In your Interface

@GET Call loadProfile(@Url String url);

From Where you call the server add this function.

  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) {

        }
    });
}

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

}

taman neupane
  • 938
  • 9
  • 18