Reputation: 6114
As mentioned in question, I need to construct an API interface for retrofit. This is my URL :
https://api.flightstats.com/flex/weather/rest/v1/json/all/COK?appId=XXXXXXXXX&appKey=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&codeType=IATA
//--------------------------------------------------------^^^--------------------------------------------------------------------
As marked above, the problem lies at the position COK
, which is an airport code, and I need to pass it in the request, however I am unable to pass it in my implementation of retrofit's interface creation. This is my code:
WeatherApiClient.class
public class WeatherApiClient {
public static final String BASE_URL = "https://api.flightstats.com/flex/weather/rest/v1/json/all/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
WeatherApiInterface.class
public interface WeatherApiInterface {
@GET("COK") // this should be dynamic
Call<WeatherPojo> getValues(@Query("appId") String appId, @Query("appKey") String key, @Query("codeType") String code);
}
How should I change my code so that I can pass the airport's code also while making a request?
Upvotes: 0
Views: 1155
Reputation: 6828
Try this intreface
code,
public interface WeatherApiInterface{
@GET("{id}")
Call<WeatherPojo> getValues(@Path("id") String airportId, @Query("appId") String appId, @Query("appKey") String key, @Query("codeType") String code);
}
You haven't shown how you are invoking this. So I assume it is fine.
Upvotes: 0
Reputation: 4425
As your question you have already created Retrofit instance.
public static final String BASE_URL = "https://api.flightstats.com/flex/weather/rest/v1/json/all/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
You also mention in your code a Endpoint.This Encode detail about the parameter and request method.
public interface WeatherApiInterface
{ @GET("COK")
//----^^^--
Call<WeatherPojo> getValues(@Query("appId") String appId, @Query("appKey") String key, @Query("codeType") String code);
}
But missing part in your code it is your a async operation. Put this code in your activity class.
ApiInterface apiService =
ApiClient.getClient().create(WeatherApiInterface.class);
Call<WeatherPojo> call = apiService.getValues(appId,appKey,codeType);
call.enqueue(new Callback<WeatherPojo>() {
@Override
public void onResponse(Call<WeatherPojo>call, Response<WeatherPojo> response) {
List<Movie> movies = response.body().getResults();
Log.d(TAG, "Number of movies received: " + movies.size());
}
@Override
public void onFailure(Call<WeatherPojo>call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
});
Upvotes: 1