Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 改装2:将动态收割台与车身一起传递_Android_Retrofit2 - Fatal编程技术网

Android 改装2:将动态收割台与车身一起传递

Android 改装2:将动态收割台与车身一起传递,android,retrofit2,Android,Retrofit2,我想动态地将头和体传递给Web Api。因此,我实施了以下措施: public interface NotificationService { @POST("user/update/notification") Call<JsonObject> notification(@Header("Authorization") String authorization, @Body NotificationRequest notificationRequest); } Ok

我想动态地将头和体传递给Web Api。因此,我实施了以下措施:

public interface NotificationService {
    @POST("user/update/notification")
    Call<JsonObject> notification(@Header("Authorization") String authorization, @Body NotificationRequest notificationRequest);
}
OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .cache(cache);
builder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder ongoing = chain.request().newBuilder();
            ongoing.addHeader("Authorization", getToken(app));
            return chain.proceed(ongoing.build());
        }
    });
公共接口通知服务{
@发布(“用户/更新/通知”)
调用通知(@Header(“Authorization”)字符串Authorization、@Body NotificationRequest NotificationRequest);
}
并以此作为

showProgressDialog();
NotificationRequest notificationRequest = new NotificationRequest(checked ? ApiConstants.IS_ON : ApiConstants.IS_OFF, getUserId());
NotificationService notificationService = ApiFactory.provideNotificationService();
Call<JsonObject> call = notificationService.notification(getAuthorizationHeader(), notificationRequest);
call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                logDebug(SettingsFragment.class, response.body().toString());
                hideProgressDialog();
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                hideProgressDialog();
            }
        });
showProgressDialog();
NotificationRequest NotificationRequest=新的NotificationRequest(选中?ApiconStats.IS_打开:ApiconStats.IS_关闭,getUserId());
NotificationService NotificationService=apFactory.provideNotificationService();
Call Call=notificationService.notification(getAuthorizationHeader(),notificationRequest);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
logDebug(setingsFragment.class,response.body().toString());
hideProgressDialog();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
hideProgressDialog();
}
});
但这样,我就不会得到null响应(
response.body()
为null)

有人能建议如何将动态标题和正文一起传递吗


注意:我阅读了教程,但没有找到同时通过两个的方法。

据我所知,没有办法同时通过页眉和正文

但您可以将
Interceptor
添加到
OkHttpClient
中,如下所示:

public interface NotificationService {
    @POST("user/update/notification")
    Call<JsonObject> notification(@Header("Authorization") String authorization, @Body NotificationRequest notificationRequest);
}
OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .cache(cache);
builder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder ongoing = chain.request().newBuilder();
            ongoing.addHeader("Authorization", getToken(app));
            return chain.proceed(ongoing.build());
        }
    });
这将在每个请求中添加授权标头。您可以控制将头添加到某些条件,例如,若用户已登录,则只应添加请求头

只需将if条件下的行换行,如下所示:

if(isUserLoggedIn())
    ongoing.addHeader("Authorization", getToken(app));

就我所见,没有办法同时通过头球和身体

但您可以将
Interceptor
添加到
OkHttpClient
中,如下所示:

public interface NotificationService {
    @POST("user/update/notification")
    Call<JsonObject> notification(@Header("Authorization") String authorization, @Body NotificationRequest notificationRequest);
}
OkHttpClient.Builder builder = new OkHttpClient.Builder()
            .cache(cache);
builder.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder ongoing = chain.request().newBuilder();
            ongoing.addHeader("Authorization", getToken(app));
            return chain.proceed(ongoing.build());
        }
    });
这将在每个请求中添加授权标头。您可以控制将头添加到某些条件,例如,若用户已登录,则只应添加请求头

只需将if条件下的行换行,如下所示:

if(isUserLoggedIn())
    ongoing.addHeader("Authorization", getToken(app));

您正在使用2。完全可以使用动态标题,例如:

    @POST("hello-world")
    fun getKaboom(
        @Body body: TheBody,
        @Header("hello-world-header") helloWorldHeader: String? = "kaboom"
    ): Single<KaboomResponse>
@POST(“你好,世界”)
乐趣之旅(
@身体:身体,
@标题(“hello world标题”)helloWorldHeader:字符串?=“kaboom”
):单身

您正在使用2。完全可以使用动态标题,例如:

    @POST("hello-world")
    fun getKaboom(
        @Body body: TheBody,
        @Header("hello-world-header") helloWorldHeader: String? = "kaboom"
    ): Single<KaboomResponse>
@POST(“你好,世界”)
乐趣之旅(
@身体:身体,
@标题(“hello world标题”)helloWorldHeader:字符串?=“kaboom”
):单身

请检查此anwser@RobertEstivill我已动态传递标头,如您的答案所示。请检查此anwser@RobertEstivill我已动态传递标头,如您的答案所示。