Search code examples
androidjsongridviewgetjson

How to handle access token?


I am novice android developer and developing app for the first time so please be gentle if i am making any mistake in asking question.

I have API Document which is attached (Consist of Access Token) and i want to call category Using GetCateogry method as stated in the APIdocument also want to display the Category in the Gridview.

I have tried different methods but enable to figure out how to display the Json Response to GridView. I am bit confuse about how to handle Access token using GET & Post method.

Any help would be Highly Appreciated.

Thanks in advance.

Postman showing Getcategory

image1

API Dcoument

image2

Access Token

image3


Solution

  • If you're using a library like Retrofit to manage your network requests (which I highly recommend), you could easily add the token as header to your request with:

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  
    httpClient.addInterceptor(new Interceptor() {  
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();
    
            // Request customization: add request headers
            Request.Builder requestBuilder = original.newBuilder()
                .header("Authentication", "accessToken"); // <-- this is the important line
    
            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });
    
    OkHttpClient client = httpClient.build();  
    

    Check out this tutorial for further reading.