如何在android应用的REST API代码中使用字符串?

时间:2020-11-27 07:14:10

I'm building an Android app to that consumes a REST API. I'm using OkHttp and the API I'm hitting requires an API key. I thought it would be best practice to store my API key in my strings.xml file. I also thought it was a best practice to not include Android sdk specific code, now I'm realizing these two thoughts might be conflicting.

我正在构建一个使用REST API的Android应用程序。我正在使用OkHttp,而我正在访问的API需要一个API键。我认为最好的做法是在字符串中存储API键。xml文件。我还认为不包含Android sdk特定代码是一种最佳实践,现在我意识到这两种想法可能相互冲突。

I'm building the app using an MVP architecture and in my NetModule class I have a method provideOkHttpClient(Cache) that looks like this:

我正在使用MVP架构构建应用程序,在我的NetModule类中,我有一个方法provideOkHttpClient(缓存),它看起来是这样的:

@Provides
@Singleton
OkHttpClient provideOkhttpClient (Cache cache){
    OkHttpClient.Builder client = new OkHttpClient.Builder();
    client.cache(cache);

    // Add GiantBomb.com api key to request
    client.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            HttpUrl originalHttpUrl = original.url();

            HttpUrl url = originalHttpUrl.newBuilder()
                    .addQueryParameter("apikey", MY_API_KEY)
                    .build();

            // Request customization: add request headers
            Request.Builder requestBuilder = original.newBuilder()
                    .url(url);

            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    });

    return client.build();
}

I got the Interceptor code from the "Add Query Parameters" section here

我从这里的“添加查询参数”部分得到了截取程序代码。

I'm wondering what the best way to store my API key is. This key should never change and since I'm only using a single website's API for this app right now it will be in all the requests. I did try to put the API key in my strings.xml but I do not have a Context available to call getString(). Of course I could just store the API key in a local string but I'm not sure that's the best method. Any help is appreciated!

我想知道存储API密钥的最好方法是什么。这个键永远不会改变,因为我现在只使用一个网站的API,它会出现在所有的请求中。我试过把API键放到字符串中。但我没有可用的上下文来调用getString()。当然,我可以将API键存储在本地字符串中,但我不确定这是否是最好的方法。任何帮助都是赞赏!

Here is the full NetModule.java class in case it helps. Here I am trying to call the getString method, but there is no Context and I'm not sure I can even get a Context. If it's not already obvious, this is my first foray into MVP, and only my first Android app after taking Udacity's Developing Android Apps course.

这是完整的NetModule。java类,以防它有帮助。这里我尝试调用getString方法,但是没有上下文,我也不确定我是否能得到上下文。如果还不明显的话,这是我第一次尝试MVP,也是我在Udacity开发Android应用程序课程后的第一个Android应用。

1 个解决方案

#1


1  

Since you are already using Dagger, why not use it to inject the API key into your NetModule class?

既然已经在使用Dagger,为什么不使用它将API键注入到NetModule类中呢?

#1


1  

Since you are already using Dagger, why not use it to inject the API key into your NetModule class?

既然已经在使用Dagger,为什么不使用它将API键注入到NetModule类中呢?