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());
}
});
必须输入账号和密码测试
网友评论