反应本机& Android上的okhttp-设置用户代理 [英] React Native & okhttp on Android - Set User-Agent

查看:285
本文介绍了反应本机& Android上的okhttp-设置用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android上使用React Native设置User-Agent.做了一些研究,看来我应该使用okhttp Interceptor .我找到的示例说明了如何完成此操作(链接),但是我不确定如何注册拦截器.

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. An example that I've found explains how this should be done(Link) but then I am not sure on how to register the Interceptor.

所以要设置User-Agent,我正在使用此类:

So in order to set the User-Agent I am using this class:

public class CustomInterceptor implements Interceptor {
    @Override public Response intercept(Interceptor.Chain chain) throws IOException {
      Request originalRequest = chain.request();
      Request requestWithUserAgent = originalRequest.newBuilder()
          .removeHeader("User-Agent")
          .header("User-Agent", "Trevor")
          .build();
      return chain.proceed(requestWithUserAgent);
    }
}

那么剩下的就是注册上面的拦截器,那么应​​该在哪里做呢?也许在MainActivity.java中?

Then what's left is to register the above interceptor so where it should be done? Maybe in MainActivity.java?

OkHttpClient okHttp = new OkHttpClient();
okHttp.interceptors().add(new CustomInterceptor());

构建应用程序时没有出现任何错误,因此我认为CustomInterceptor应该很好-只需使应用程序使用它即可.

I am not getting any errors when building the app so I think that the CustomInterceptor should be fine - just need to make the app use it.

更新: 我目前正在尝试在MainActivity中注册拦截器,但不会接收它:

UPDATE: I'm currently trying to register the interceptor in MainActivity but it won't pick it up:

public class MainActivity extends ReactActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    OkHttpClient client = new OkHttpClient();
    client.networkInterceptors().add(new CustomInterceptor());

  };

};

推荐答案

所以我终于弄清楚了.这是使用React Native覆盖okhttp的User-Agent的解决方案.

So I've finally figured it out. Here is the solution for overriding the User-Agent of okhttp with React Native.

创建一个名为CustomInterceptor.java的文件:

package com.trevor;

import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class CustomInterceptor implements Interceptor {

    public CustomInterceptor() {}

    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request originalRequest = chain.request();
        Request requestWithUserAgent = originalRequest.newBuilder()
            .removeHeader("User-Agent")
            .addHeader("User-Agent", "Trevor")
            .build();

        return chain.proceed(requestWithUserAgent);
    }

}

然后用MainActivity.java 覆盖 onCreate方法:

...
import com.facebook.react.modules.network.OkHttpClientProvider;
...

public class MainActivity extends ReactActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        attachInterceptor();
    }

    private void attachInterceptor() {
        OkHttpClient client = OkHttpClientProvider.getOkHttpClient();
        client.networkInterceptors().add(new CustomInterceptor());
    }
}

请注意,我正在导入com.facebook.react.modules.network.OkHttpClientProvider; 并覆盖该客户端,而不是创建普通的OkHttpClient,因为这是React Native会使用的版本.

Note that I'm importing com.facebook.react.modules.network.OkHttpClientProvider; and overriding that client instead of creating a vanilla OkHttpClient since this is the one that React Native will use.

这篇关于反应本机& Android上的okhttp-设置用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆