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

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

问题描述

我正在尝试在 Android 上使用 React Native 设置 User-Agent.做了一些研究,看起来我应该使用 okhttp 拦截器.我发现的一个例子解释了如何做到这一点(链接)但我不确定如何注册拦截器.

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 覆盖 okhttp3 的 User-Agent 的解决方案.

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

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

package com.trevor;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.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天全站免登陆