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);
热门推荐
5 简短元宵节祝福语
10 升班祝福语简短10字
11 婚礼红包祝福语简短霸气
12 升学了祝福语简短句
13 社团迎新祝福语简短英语
14 晚来的生日祝福语简短
15 生日祝福语已婚朋友简短
16 新郎致辞诗句祝福语简短
17 朋友新工作祝福语简短
18 祝福语结婚准备礼物简短