Android 标头和正文:一个身份验证示例
示例
该@Header和@Body注释可以被放置到方法签名,并根据您的模型改造将自动创建它们。
public interface MyService {
@POST("authentication/user")
Call<AuthenticationResponse> authenticateUser(@Body AuthenticationRequest request, @Header("Authorization") String basicToken);
}AuthenticaionRequest是我们的模型,即POJO,其中包含服务器所需的信息。对于此示例,我们的服务器需要客户端密钥和机密。
public class AuthenticationRequest {
String clientKey;
String clientSecret;
}注意,在@Header("Authorization")我们指定的过程中,我们正在填充Authorization标头。其他标题将自动填充,因为Retrofit可以根据我们发送并期望返回的对象类型推断它们的含义。
我们在某个地方创建改造服务。我们确保使用HTTPS。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https:// some example site")
.client(client)
.build();
MyService myService = retrofit.create(MyService.class)然后,我们可以使用我们的服务。
AuthenticationRequest request = new AuthenticationRequest(); request.setClientKey(getClientKey()); request.setClientSecret(getClientSecret()); String basicToken = "Basic " + token; myService.authenticateUser(request, basicToken);
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短