React Native & okhttp on Android - Set User-Agent

前端 未结 4 1034
梦如初夏
梦如初夏 2020-12-10 15:42

I\'m trying to set the User-Agent with React Native on Android. Did some research and it looks like I should use an okhttp Interceptor

4条回答
  •  误落风尘
    2020-12-10 16:08

    I've implemented this functionality using OkHttp and my code is pretty the same as yours - and everything works fine.

    Consider using addHeader("User-Agent", "Trevor") instead of header("User-Agent", "Trevor"), because the latter will replace all of already set headers.

    I'm using okHttp.networkInterceptors().add(new CustomInterceptor()); instead of okHttp.interceptors().add(new CustomInterceptor());, but I don't think it's a matter of concern here.

    Update I do it in onCreate() method too. Everything works as it should.

提交回复
热议问题