android - How to use Authorization Token token=A-123456789qwertyuio12 Header in Retrofit 2.0 -
im trying consume api has authorization header, can 200 response in postman data cant work in retrofit
may need add token
using okhttp interceptor.
okhttpclient client = new okhttpclient.builder() .addnetworkinterceptor(mtokeninterceptor) .build();
then add retrofit
:
retrofit retrofit = new retrofit.builder() .client(client) .baseurl(base_url) .build();
the mtokeninterceptor
:
interceptor mtokeninterceptor = new interceptor() { @override public response intercept(chain chain) throws ioexception { request request = chain.request(); if (mtoken != null) { request.builder requestbuilder = request.newbuilder() .addheader("authorization", mtoken); request newrequest = requestbuilder.build(); return chain.proceed(newrequest); } return chain.proceed(request); } };
when token
, assign mtoken
,
Comments
Post a Comment