如何在改造中使用基本身份验证发送发帖请求?

car*_*rld 5 android header basic-authentication retrofit

在我的代码中,我想发送带有基本身份验证的帖子请求。

这是我的邮递员屏幕截图:

在此处输入图片说明

这是我的apiInterface类

@FormUrlEncoded
    @POST("GetBarcodeDetail")
    Call<PreliminaryGoodsAcceptResponse> PRELIMINARY_GOODS_ACCEPT_RESPONSE_CALL(@Field("ProcName") String procName, @Field("Barcode") String barcode, @Field("LangCode") String langCode);
Run Code Online (Sandbox Code Playgroud)

这是我的apiclient

public class ApiClient {

    public static final String BASE_URL = "http://192.**********";
    private static Retrofit retrofit = null;
    private static OkHttpClient sClient;

    public static Retrofit getClient() {
        if(sClient == null) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            sClient = new OkHttpClient.Builder()
                    .addInterceptor(new HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT))
                    .addInterceptor(interceptor)
                    .build();
        }

        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(sClient)
                    .build();
        }
        return retrofit;
    }

}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何使用标头发送帖子请求:

标头用户名:EBA令牌:
34242353453456563DSFS

Aji*_*ana 9

到目前为止,这是我尝试过的“基本身份验证”中最简单的方法。

使用以下代码生成auth标头(API /存储库类)

 var basic = Credentials.basic("YOUR_USERNAME", "YOUR_PASSWORD")
Run Code Online (Sandbox Code Playgroud)

将此作为标头传递给Webservice调用(API /存储库类)

 var retrofitCall = myWebservice.getNewsFeed(basic)
Run Code Online (Sandbox Code Playgroud)

将基本标头添加为参数(Retrofit Webservice接口类)

 @GET("newsfeed/daily")
 fun getNewsFeed(@Header("Authorization") h1:String):Call<NewsFeedResponse>
Run Code Online (Sandbox Code Playgroud)

抱歉,我的代码在Kotlin中,但可以轻松转换为Java。

参考:https : //mobikul.com/basic-authentication-retrofit-android/

  • 这是迄今为止最好的答案 (2认同)

And*_*eam 5

像这样制作标题..

 private Retrofit getClient(final Context context) {

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.readTimeout(60, TimeUnit.SECONDS);
    client.writeTimeout(60, TimeUnit.SECONDS);
    client.connectTimeout(60, TimeUnit.SECONDS);
    client.addInterceptor(interceptor);
    client.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (context == null) {
                request = request
                        .newBuilder()
                        .build();
            } else {
                request = request
                        .newBuilder()
                        .addHeader("Authorization", "Bearer " + AppSetting.getStringSharedPref(context, Constants.USER_KEY_TOKEN, ""))
                        .build();
            }
            return chain.proceed(request);
        }
    });

    retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build();


    return retrofit;
}
Run Code Online (Sandbox Code Playgroud)