Java android 2无法添加标题(415错误代码)

Java android 2无法添加标题(415错误代码),java,android,retrofit2,okhttp3,skyscanner,Java,Android,Retrofit2,Okhttp3,Skyscanner,我正试图通过skyscanner api访问票证数据并将其传递给我的视图,但我无法实现这一点,因为我得到了415个错误代码,我正在使用改型2并以编程方式添加标题。我的界面如下所示: public interface GetFlightDetails { @POST("apiservices/pricing/v1.0/") Call<TicketData> getFlightList(@Query("apiKey") String apiKey,

我正试图通过skyscanner api访问票证数据并将其传递给我的视图,但我无法实现这一点,因为我得到了415个错误代码,我正在使用改型2并以编程方式添加标题。我的界面如下所示:

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) ;
}
在我的活动中,当我准备提出请求时,我有以下代码:

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");
        }
    });
在我的日志文件中,我看到以下错误:

com.example.ex D/OkHttp: --> POST http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=xxxxxxxx&country=US&currency=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&currency=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&currency=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}
我不知道为什么会发生这种情况,因为我已经尝试在接口中添加头

Skyscanner文档和


谢谢

请参考相同类型的示例:检查您的参数类型,哪个字段是查询字符串,哪个字段是@field等等

公共静态最终字符串行程\u API=http://business.skyscanner.net/; 公共接口{ @FormUrlEncoded @HeadersContent类型:application/x-www-form-urlencoded;charset=UTF-8 @POST/apiservices/pricing/v1.0/ 拉维拉皮酒店 @Fieldcountry字符串国家, @字段货币字符串货币, @Fieldlocale字符串区域设置, @FieldlocationSchema字符串locationSchema, @字段apikey字符串apikey, @Fieldgrouppricing字符串grouppricing, @Fieldoriginplace字符串originplace, @Fielddestinationplace字符串destinationplace, @Fieldoutbounddate字符串outbounddate, @Fieldinbounddate字符串inbounddate, @现场成人, @Fieldchildren和int children, @现场婴儿, @Fieldcabinclass字符串cabinclass,回调响应; } TravelApiInterface currencyRequestInterfaceService

    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(Url.TRAVEL_API)
            .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addQueryParam(getString(R.string.api_key_title), getString(R.string.api_key_value));
                }
            })
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    currencyRequestInterfaceService = adapter.create(TravelApiInterface.class);
==================== 样品请求

requestConversion("UK", "GBP", "en-GB", "iata", "xxxxxxxx","on", "EDI", "LHR", 
    "2016-10-04", "2016-10-11", 1, 0, 0, "Economy", new Callback<Object>() {

                @Override
                public void success(Object o, Response response) {

                }

                @Override
                public void failure(RetrofitError error) {

                }
});
在strings.xml中

<string name="api_key_title">apikey</string>
<string name="api_key_value">xxxxxxx</string>

请参考相同类型的示例:检查您的参数类型,哪个字段是查询字符串,哪个字段是@field等等

公共静态最终字符串行程\u API=http://business.skyscanner.net/; 公共接口{ @FormUrlEncoded @HeadersContent类型:application/x-www-form-urlencoded;charset=UTF-8 @POST/apiservices/pricing/v1.0/ 拉维拉皮酒店 @Fieldcountry字符串国家, @字段货币字符串货币, @Fieldlocale字符串区域设置, @FieldlocationSchema字符串locationSchema, @字段apikey字符串apikey, @Fieldgrouppricing字符串grouppricing, @Fieldoriginplace字符串originplace, @Fielddestinationplace字符串destinationplace, @Fieldoutbounddate字符串outbounddate, @Fieldinbounddate字符串inbounddate, @现场成人, @Fieldchildren和int children, @现场婴儿, @Fieldcabinclass字符串cabinclass,回调响应; } TravelApiInterface currencyRequestInterfaceService

    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(Url.TRAVEL_API)
            .setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addQueryParam(getString(R.string.api_key_title), getString(R.string.api_key_value));
                }
            })
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    currencyRequestInterfaceService = adapter.create(TravelApiInterface.class);
==================== 样品请求

requestConversion("UK", "GBP", "en-GB", "iata", "xxxxxxxx","on", "EDI", "LHR", 
    "2016-10-04", "2016-10-11", 1, 0, 0, "Economy", new Callback<Object>() {

                @Override
                public void success(Object o, Response response) {

                }

                @Override
                public void failure(RetrofitError error) {

                }
});
在strings.xml中

<string name="api_key_title">apikey</string>
<string name="api_key_value">xxxxxxx</string>

我们需要为这个问题提供内容类型,并且需要作为模型类传递

@Headers("Content-Type:application/json")
@POST("saveAddressByFE")
Call<ChangeAddressModel> updateAddress(@Body AddressModel addressModel );
}

我们将要发送的数据模型classAddressModel addressModel

public class AddressModel {
private String addressType,address,custId;

public AddressModel(String addressType, String address, String custId) {
    this.addressType = addressType;
    this.address = address;
    this.custId = custId;
}

public String getAddressType() {
    return addressType;
}

public void setAddressType(String addressType) {
    this.addressType = addressType;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCustId() {
    return custId;
}

public void setCustId(String custId) {
    this.custId = custId;
}

}

我们需要为这个问题提供内容类型,并且需要作为模型类传递

@Headers("Content-Type:application/json")
@POST("saveAddressByFE")
Call<ChangeAddressModel> updateAddress(@Body AddressModel addressModel );
}

我们将要发送的数据模型classAddressModel addressModel

public class AddressModel {
private String addressType,address,custId;

public AddressModel(String addressType, String address, String custId) {
    this.addressType = addressType;
    this.address = address;
    this.custId = custId;
}

public String getAddressType() {
    return addressType;
}

public void setAddressType(String addressType) {
    this.addressType = addressType;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCustId() {
    return custId;
}

public void setCustId(String custId) {
    this.custId = custId;
}

}

我正在使用改装2和代码。您发布了一个基于改装1的答案,您可以修改它吗?谢谢我正在使用改型2和代码你发布了一个基于改型1的答案,你能修改它吗?谢谢提供此链接以获取答案请参考此链接以获取答案