安卓 Authorization 认证

OkHttpClient okHttpClient = new OkHttpClient
        .Builder()
        .authenticator(new Authenticator() {
            @Nullable
            @Override
            public Request authenticate(@Nullable Route route, @NotNull Response response) {

                return response.request().newBuilder()
                        .header("Authorization", credential)
                        .build();
            }
        })
        .build();

MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create("", JSON);
final Request request = new Request.Builder()
        .url(loginPostUrL)
        .addHeader("Charset", "UTF-8")
        .addHeader("Content-Type", "application/json")
        .addHeader("Authorization","Basic d2ViYXBwOndlYmFwcA==")
        .post(body)
        .build();


Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
    @Override
    public void onFailure(@NotNull Call call, @NotNull IOException e) {

    }

    @Override
    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

        Log.d("数据", response.body().string());
    }
});

必须输入账号和密码测试

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。