I'm trying to access ticket data via skyscanner api and pass it to my view, but I cannot accomplish that, because I get 415 error code I'm using retrofit2 and adding header programmatically. My interface looks like this:
public interface GetFlightDetails {
@POST("apiservices/pricing/v1.0/")
Call<TicketData> getFlightList(@Query("apiKey") String apiKey,
@Query("country") String country,
@Query("currency") String currency,
@Query("locale") String locale,
@Query("originPlace") String originPlace,
@Query("destinationPlace") String destinationPlace,
@Query("outboundPartialDate")String outboundPartialDate,
@Query("inboundPartialDate") String inboundPartialDate,
@Query("locationschema") String locationschema,
@Query("cabinclass") String cabinclass,
@Query("adults") int adults,
@Query("children") int children,
@Query("infants") int infants,
@Query("groupPricing") boolean groupPricing) ;
}
and in my activity, when I'm ready to make a request I have the following code:
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
.create();
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
//adding logging
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BASIC);
httpClient.interceptors().add(logging);
//headers
httpClient.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request original = chain.request();
//adding header info
Request request = original.newBuilder()
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Accept", "application/json")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(httpClient.build())
.build();
GetFlightDetails api = retrofit.create(GetFlightDetails.class);
Call<TicketData> mresponse = api
.getFlightList(API_KEY, country, currency, locale, from, to,
departDate.substring(0,10), returnDate.substring(0,10),
locationSchema, cabinClass, adult, children, infants, false);
mresponse.enqueue(new Callback<TicketData>()
{
@Override
public void onResponse(Call<TicketData> call, Response <TicketData> response) {
if (!response.isSuccessful()){
Log.d("UnSuccess", response.raw().toString());
return;
}
else {
progress.cancel(); //cancel progress dialog
Log.d("Success", response.raw().toString());
TicketData ticketData = response.body();
RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext(), ticketData);
mRecyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<TicketData> call, Throwable t){
progress.setMessage("Retrofit Error Occured");
}
});
and in my log file I see the following error:
com.example.ex D/OkHttp: --> POST http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US¤cy=USD&locale=en-us&originPlace=SFO&destinationPlace=LAX&outboundPartialDate=2016-10-24&inboundPartialDate=2016-10-31&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false http/1.1 (0-byte body)
com.example.ex D/OkHttp: <-- 415 Unsupported Media Type http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US¤cy=USD&locale=en-us&originPlace=SFO&destinationPlace=LAX&outboundPartialDate=2016-10-24&inboundPartialDate=2016-10-31&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false (403ms, 0-byte body)
com.example.ex D/UnSuccess: Response{protocol=http/1.1, code=415, message=Unsupported Media Type, url=http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US¤cy=USD&locale=en-us&originPlace=SFO&destinationPlace=LAX&outboundPartialDate=2016-10-24&inboundPartialDate=2016-10-31&locationschema=iata&cabinclass=Economy&adults=1&children=0&infants=0&groupPricing=false}
I'm not sure why it occurs, because I've tried to add headers in my interface.
Skyscanner docs reference 1 and reference 2
Thanks!