使用 Glide 和 okhttp3 在 Stetho 中进行网络检查
Network Inspection in Stetho with Glide and okhttp3
如何结合Glide和Stetho使用okhttp3调试图片加载系统
我做了以下事情
1.Added 依赖项
//stetho
compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
//glide
compile 'com.github.bumptech.glide:glide:3.7.0'
// Glide's OkHttp3 Integration
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
//okhttp3
compile 'com.squareup.okhttp3:okhttp:3.2.0'
1.Added初始化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build());
}
}
3.add 滑动配置以使用 okhttp3
/**
* Created by Arnaud Camus Copied by MOMANI on 2016/04/15.
* http://arnaud-camus.fr/combining-glide-and-stetho-to-easily-debug-your-image-loading-system/
*/
public class StethoOkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) { }
@Override
public void registerComponents(Context context, Glide glide) {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
client.networkInterceptors().add(new com.facebook.stetho.okhttp3.StethoInterceptor());
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
4.added GlideModule
AndroidManifest.xml
中的元数据标签
<application
android:name=".....">
.....
<meta-data android:name="android.alcode.com.material.utils.glide.StethoOkHttpGlideModule"
android:value="GlideModule" />
</application>
5.Glide 加载图像行
Glide.with(((ViewHolderSmall) holder).imageView.getContext())
.load(post.getImageUrl())
// .diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(((ViewHolderSmall) holder).imageView);
当我打开 chrome 开发者工具时 Resources Tab
可以正常工作,但 network tab
不能!!!
为什么?我的错误在哪里?是否建议将 okhttp3
与 Glide 一起使用?以及如何在不使用 okhttp3
的情况下连接它
任何重复或链接都会有所帮助
Re: is it recommended to use okhttp3 with Glide?
是的,这就是 OkHttp 集成的方式。
Re: why? where is my mistake?
这可能是需要的,因为内置的 GlideModule 可能会覆盖您拦截的客户端(GlideModule 执行之间没有定义的顺序)。考虑来自 wiki 的以下内容:
When overriding default behaviour make sure your custom GlideModule is registered in the manifest and the default one is excluded. Exclusion may mean removing the corresponding metadata from the manifest or using the jar dependency instead of the aar one. -- Overriding default behaviour - Glide Wiki
基于 Conflicting GlideModules - Glide Wiki:从 okhttp3-integration
依赖项中删除 @aar
,或添加到清单:
<meta-data android:name="com.bumptech.glide.integration.okhttp3.OkHttpGlideModule" tools:node=”remove” />
还要确保您没有被缓存所愚弄:.diskCacheStrategy(NONE).skipMemoryCache(true)
;您可以在看到请求后立即将其删除。
OkHttp3 更改了 API client.networkInterceptors()
不再可写:
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder()
.addNetworkInterceptor(new com.facebook.stetho.okhttp3.StethoInterceptor())
.build();
终于成功了
根据Glide Wikis我改了下面
<application
android:name=".....">
.....
<meta-data android:name="android.alcode.com.material.utils.glide.StethoOkHttpGlideModule"
android:value="GlideModule" />
<meta-data android:name="com.bumptech.glide.integration.okhttp3.OkHttpGlideModule" tools:node="remove" />
</application>
并根据 我更改了以下内容
public class StethoOkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) { }
@Override
public void registerComponents(Context context, Glide glide) {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder()
.addNetworkInterceptor(new com.facebook.stetho.okhttp3.StethoInterceptor())
.build();
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
that's it
但一些关于 okhttp3
的问题没有回答
Re: how how to connect it without using okhttp3
默认(内置)网络集成使用 HttpUrlGlideUrlLoader
创建的 HttpUrlFetcher
,因此为了与 Stetho 集成,您需要将其替换为拦截请求的网络集成。
'
滑行模块
@Override public void registerComponents(Context context, Glide glide) {
glide.register(GlideUrl.class, InputStream.class, new StethoHttpUrlGlideUrlLoader.Factory());
}
确保 StethoHttpUrlGlideUrlLoader
returns 修改后的 类 而不是 build
和 getResourceFetcher
.
中内置的 类
自定义获取器
为简洁起见,此处仅包含结构差异,应该清楚这些行来自上下文的位置。完整代码可在 GitHub:
public class StethoHttpUrlFetcher implements DataFetcher<InputStream> {
private final StethoURLConnectionManager stethoManager;
public StethoHttpUrlFetcher(GlideUrl glideUrl) {
...
this.stethoManager = new StethoURLConnectionManager("Glide");
}
private InputStream loadDataWithRedirects(...) {
...
//urlConnection.setRequestProperty("Accept-Encoding", "gzip"); // don't request, it's wasteful for images
...
stethoManager.preConnect(urlConnection, null);
try {
// Connect explicitly to avoid errors in decoders if connection fails.
urlConnection.connect();
stethoManager.postConnect();
} catch (IOException ex) {
stethoManager.httpExchangeFailed(ex);
throw ex;
}
...
}
private InputStream getStreamForSuccessfulRequest(HttpURLConnection urlConnection) throws IOException {
try {
InputStream responseStream = stethoManager.interpretResponseStream(urlConnection.getInputStream());
if (TextUtils.isEmpty(urlConnection.getContentEncoding())) {
...
stream = ContentLengthInputStream.obtain(responseStream, contentLength);
} else {
...
stream = responseStream;
}
return stream;
} catch (IOException ex) {
stethoManager.httpExchangeFailed(ex);
throw ex;
}
}
@Override public void cleanup() {
...
if (isCancelled) { // otherwise it stays pending indefinitely because the stream is not read
stethoManager.httpExchangeFailed(new IOException("Cancelled"));
}
}
}
如何结合Glide和Stetho使用okhttp3调试图片加载系统
我做了以下事情
1.Added 依赖项
//stetho
compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
//glide
compile 'com.github.bumptech.glide:glide:3.7.0'
// Glide's OkHttp3 Integration
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
//okhttp3
compile 'com.squareup.okhttp3:okhttp:3.2.0'
1.Added初始化
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
.build());
}
}
3.add 滑动配置以使用 okhttp3
/**
* Created by Arnaud Camus Copied by MOMANI on 2016/04/15.
* http://arnaud-camus.fr/combining-glide-and-stetho-to-easily-debug-your-image-loading-system/
*/
public class StethoOkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) { }
@Override
public void registerComponents(Context context, Glide glide) {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient();
client.networkInterceptors().add(new com.facebook.stetho.okhttp3.StethoInterceptor());
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
4.added GlideModule
AndroidManifest.xml
<application
android:name=".....">
.....
<meta-data android:name="android.alcode.com.material.utils.glide.StethoOkHttpGlideModule"
android:value="GlideModule" />
</application>
5.Glide 加载图像行
Glide.with(((ViewHolderSmall) holder).imageView.getContext())
.load(post.getImageUrl())
// .diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(((ViewHolderSmall) holder).imageView);
当我打开 chrome 开发者工具时 Resources Tab
可以正常工作,但 network tab
不能!!!
为什么?我的错误在哪里?是否建议将 okhttp3
与 Glide 一起使用?以及如何在不使用 okhttp3
任何重复或链接都会有所帮助
Re: is it recommended to use okhttp3 with Glide?
是的,这就是 OkHttp 集成的方式。
Re: why? where is my mistake?
这可能是需要的,因为内置的 GlideModule 可能会覆盖您拦截的客户端(GlideModule 执行之间没有定义的顺序)。考虑来自 wiki 的以下内容:
When overriding default behaviour make sure your custom GlideModule is registered in the manifest and the default one is excluded. Exclusion may mean removing the corresponding metadata from the manifest or using the jar dependency instead of the aar one. -- Overriding default behaviour - Glide Wiki
基于 Conflicting GlideModules - Glide Wiki:从 okhttp3-integration
依赖项中删除 @aar
,或添加到清单:
<meta-data android:name="com.bumptech.glide.integration.okhttp3.OkHttpGlideModule" tools:node=”remove” />
还要确保您没有被缓存所愚弄:.diskCacheStrategy(NONE).skipMemoryCache(true)
;您可以在看到请求后立即将其删除。
OkHttp3 更改了 API client.networkInterceptors()
不再可写:
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder()
.addNetworkInterceptor(new com.facebook.stetho.okhttp3.StethoInterceptor())
.build();
终于成功了
<application
android:name=".....">
.....
<meta-data android:name="android.alcode.com.material.utils.glide.StethoOkHttpGlideModule"
android:value="GlideModule" />
<meta-data android:name="com.bumptech.glide.integration.okhttp3.OkHttpGlideModule" tools:node="remove" />
</application>
并根据
public class StethoOkHttpGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) { }
@Override
public void registerComponents(Context context, Glide glide) {
okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder()
.addNetworkInterceptor(new com.facebook.stetho.okhttp3.StethoInterceptor())
.build();
glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client));
}
}
that's it
但一些关于 okhttp3
的问题没有回答
Re: how how to connect it without using okhttp3
默认(内置)网络集成使用 HttpUrlGlideUrlLoader
创建的 HttpUrlFetcher
,因此为了与 Stetho 集成,您需要将其替换为拦截请求的网络集成。
'
滑行模块
@Override public void registerComponents(Context context, Glide glide) {
glide.register(GlideUrl.class, InputStream.class, new StethoHttpUrlGlideUrlLoader.Factory());
}
确保 StethoHttpUrlGlideUrlLoader
returns 修改后的 类 而不是 build
和 getResourceFetcher
.
自定义获取器
为简洁起见,此处仅包含结构差异,应该清楚这些行来自上下文的位置。完整代码可在 GitHub:
public class StethoHttpUrlFetcher implements DataFetcher<InputStream> {
private final StethoURLConnectionManager stethoManager;
public StethoHttpUrlFetcher(GlideUrl glideUrl) {
...
this.stethoManager = new StethoURLConnectionManager("Glide");
}
private InputStream loadDataWithRedirects(...) {
...
//urlConnection.setRequestProperty("Accept-Encoding", "gzip"); // don't request, it's wasteful for images
...
stethoManager.preConnect(urlConnection, null);
try {
// Connect explicitly to avoid errors in decoders if connection fails.
urlConnection.connect();
stethoManager.postConnect();
} catch (IOException ex) {
stethoManager.httpExchangeFailed(ex);
throw ex;
}
...
}
private InputStream getStreamForSuccessfulRequest(HttpURLConnection urlConnection) throws IOException {
try {
InputStream responseStream = stethoManager.interpretResponseStream(urlConnection.getInputStream());
if (TextUtils.isEmpty(urlConnection.getContentEncoding())) {
...
stream = ContentLengthInputStream.obtain(responseStream, contentLength);
} else {
...
stream = responseStream;
}
return stream;
} catch (IOException ex) {
stethoManager.httpExchangeFailed(ex);
throw ex;
}
}
@Override public void cleanup() {
...
if (isCancelled) { // otherwise it stays pending indefinitely because the stream is not read
stethoManager.httpExchangeFailed(new IOException("Cancelled"));
}
}
}