无法在Android中正确设置Retrofit头部。

huangapple go评论80阅读模式
英文:

Cannot properly set retrofit header in Android

问题

我有一个可以在下面的图片中看到的标题,在Postman中正常工作:

无法在Android中正确设置Retrofit头部。

但是我无法在Android中正确使用它。

到目前为止,我尝试复制的部分是 Authorization Bearer 部分是一个 @Header("Authorization") 字符串头:

Call<GPSData> call = apiService.askGPS("Authorization Bearer: " + value, gpsPost);

Call<GPSData> call = apiService.askGPS("Authorization: Bearer " + value, gpsPost);

Call<GPSData> call = apiService.askGPS("Authorization: Bearer: " + value, gpsPost);

但是它们都不起作用。

哪种是等同于在Postman中显示的头部的正确语法?

英文:

I've the header that can be seen on the following image properly working in Postman:

无法在Android中正确设置Retrofit头部。

But I'm not able to use it properly in Android.

My attempts to replicate that so far have been the following Authorization Bearer part is a @Header("Authorization") String header:

Call&lt;GPSData&gt; call = apiService.askGPS(&quot;Authorization Bearer: &quot; + value, gpsPost);

Call&lt;GPSData&gt; call = apiService.askGPS(&quot;Authorization: Bearer &quot; + value, gpsPost);

Call&lt;GPSData&gt; call = apiService.askGPS(&quot;Authorization: Bearer: &quot; + value, gpsPost);

But none of them work.

Which is the correct syntax for a header equivalent to the one shown in Postman?

答案1

得分: 1

不要将Authorization作为标头值发送,因为如果您在API服务调用参数中使用@Header("Authorization") String value,它会由Retrofit处理,而应该像这样做。

Call<GPSData> call = apiService.askGPS("Bearer: " + value, gpsPost);
英文:

don't send Authorization as the header value as it handled by retrofit if you use @Header(&quot;Authorization&quot;) String value in the api service call parameter, instead do something like this.

Call&lt;GPSData&gt; call = apiService.askGPS(&quot;Bearer: &quot; + value, gpsPost);

huangapple
  • 本文由 发表于 2020年8月5日 00:10:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63250862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定