【发布时间】:2015-09-02 11:47:12
【问题描述】:
我想知道如果我创建一个 OkHttpClient 实例来服务我的“整个 android 应用程序”,是否会出现任何性能瓶颈或问题。即在我的 Application 类中,我创建了一个包含 OkHttpClient 实例的静态公共变量,每当我需要执行 http 请求时,我基本上都会构建一个请求对象,然后使用创建的 okhttpclient 实例来触发请求。
代码是这样的
public class MyApplication extends Application {
public static OkHttpClient httpClient;
@Override
public void onCreate() {
super.onCreate();
httpClient = new OkHttpClient();
}
}
// Making request 1
Request request1 = new Request.Builder().url(ENDPOINT).build();
Response response = MyApplication.httpClient.newCall(request1).execute();
// Making request 2
Request request2 = new Request.Builder().url(ENDPOINT).build();
Response response = MyApplication.httpClient.newCall(request2).execute();
【问题讨论】: