android–Rest API– 如何添加自定义标头?

android–Rest API– 如何添加自定义标头?,第1张

概述我想用自定义标头发出POST请求.我无法使用AARestAPI–https://github.com/excilys/androidannotations/wiki/Rest%20API找到有关如何执行此 *** 作的信息.我应该使用ClientHttpRequestInterceptor,它用于经过身份验证的请求吗?https://github.com/excilys/androidannotations/w

我想用自定义标头发出POST请求.我无法使用AA Rest API – https://github.com/excilys/androidannotations/wiki/Rest%20API找到有关如何执行此 *** 作的信息.

我应该使用ClIEnthttpRequestInterceptor,它用于经过身份验证的请求吗?
https://github.com/excilys/androidannotations/wiki/Authenticated-Rest-Client

谢谢你的帮助!

解决方法:

目前有一个未解决的问题:https://github.com/excilys/androidannotations/issues/323

目前,唯一的方法是使用自定义ClIEnthttpRequestInterceptor.这是一个小例子:

@EBeanpublic class CustomheaderInterceptor implements ClIEnthttpRequestInterceptor {    @OverrIDe    public ClIEnthttpResponse intercept(httpRequest request, byte[] data, ClIEnthttpRequestExecution execution) throws IOException {        request.getheaders().add("myheader", "value");        return execution.execute(request, data);    }}

然后,您需要将它链接到restTemplate,如下所示:

@EBeanpublic class MyService {    @RestService    RestClIEnt restClIEnt;    @Bean    MobileParametersInterceptor mobileParametersInterceptor;    @AfterInject    public voID init() {        List<ClIEnthttpRequestInterceptor> interceptors = new ArrayList<ClIEnthttpRequestInterceptor>();        interceptors.add(mobileParametersInterceptor);        restClIEnt.getRestTemplate().setInterceptors(interceptors);    }}
总结

以上是内存溢出为你收集整理的android – Rest API – 如何添加自定义标头?全部内容,希望文章能够帮你解决android – Rest API – 如何添加自定义标头?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1106065.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存