关于Android应用支持IPV6

今天看了一些关于Android应用关于支持IPV6的问题,简单记录

ipv从地址来说比v4多了“[]”,长度更长

1. 正常来说OKHttp,XUtils等上层网络框架是支持ipv6的。但是如果你的应用中用到了socket,比如长连接,或者是直接用socket进行数据传输等,这时就需要进行ipv6的适配了。后面适配时再补充

2.Glide关于ipv6的支持。看到了https://www.jianshu.com/p/c8ebb4a3cc76。这篇文章介绍了绕过的方法,但看了glide的源码,发现有些不同,可能是版本不一样导致。问题原因确实如注释所写

* 

To obtain a properly escaped URL, call {@link #toURL()}. To obtain a properly escaped string * URL, call {@link #toStringUrl()}. To obtain a less safe, but less expensive to calculate cache * key, call {@link #getCacheKey()}.

但是去Glide源码中去找使用的地方,也就是register地方(这个过程可以参考上面链接内容)

Glide(
    @NonNull Context context,
    @NonNull Engine engine,
    @NonNull MemoryCache memoryCache,
    @NonNull BitmapPool bitmapPool,
    @NonNull ArrayPool arrayPool,
    @NonNull RequestManagerRetriever requestManagerRetriever,
    @NonNull ConnectivityMonitorFactory connectivityMonitorFactory,
    int logLevel,
    @NonNull RequestOptions defaultRequestOptions,
    @NonNull Map, TransitionOptions> defaultTransitionOptions,
    @NonNull List> defaultRequestListeners,
    boolean isLoggingRequestOriginsEnabled) {
  this.engine = engine;
  this.bitmapPool = bitmapPool;
  this.arrayPool = arrayPool;
  this.memoryCache = memoryCache;
  this.requestManagerRetriever = requestManagerRetriever;
  this.connectivityMonitorFactory = connectivityMonitorFactory;

  DecodeFormat decodeFormat = defaultRequestOptions.getOptions().get(Downsampler.DECODE_FORMAT);
  bitmapPreFiller = new BitmapPreFiller(memoryCache, bitmapPool, decodeFormat);

  final Resources resources = context.getResources();

  registry = new Registry();
  registry.register(new DefaultImageHeaderParser());
  // Right now we're only using this parser for HEIF images, which are only supported on OMR1+.
  // If we need this for other file types, we should consider removing this restriction.
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
    registry.register(new ExifInterfaceImageHeaderParser());
  }

  List imageHeaderParsers = registry.getImageHeaderParsers();
  Downsampler downsampler =
      new Downsampler(
          imageHeaderParsers,
          resources.getDisplayMetrics(),
          bitmapPool,
          arrayPool);
  ByteBufferGifDecoder byteBufferGifDecoder =
      new ByteBufferGifDecoder(context, imageHeaderParsers, bitmapPool, arrayPool);
  ResourceDecoder parcelFileDescriptorVideoDecoder =
      VideoDecoder.parcel(bitmapPool);
  ByteBufferBitmapDecoder byteBufferBitmapDecoder = new ByteBufferBitmapDecoder(downsampler);
  StreamBitmapDecoder streamBitmapDecoder = new StreamBitmapDecoder(downsampler, arrayPool);
  ResourceDrawableDecoder resourceDrawableDecoder =
      new ResourceDrawableDecoder(context);
  ResourceLoader.StreamFactory resourceLoaderStreamFactory =
      new ResourceLoader.StreamFactory(resources);
  ResourceLoader.UriFactory resourceLoaderUriFactory =
      new ResourceLoader.UriFactory(resources);
  ResourceLoader.FileDescriptorFactory resourceLoaderFileDescriptorFactory =
      new ResourceLoader.FileDescriptorFactory(resources);
  ResourceLoader.AssetFileDescriptorFactory resourceLoaderAssetFileDescriptorFactory =
      new ResourceLoader.AssetFileDescriptorFactory(resources);
  BitmapEncoder bitmapEncoder = new BitmapEncoder(arrayPool);

  BitmapBytesTranscoder bitmapBytesTranscoder = new BitmapBytesTranscoder();
  GifDrawableBytesTranscoder gifDrawableBytesTranscoder = new GifDrawableBytesTranscoder();

  ContentResolver contentResolver = context.getContentResolver();

  registry
      .append(ByteBuffer.class, new ByteBufferEncoder())
      .append(InputStream.class, new StreamEncoder(arrayPool))
      /* Bitmaps */
      .append(Registry.BUCKET_BITMAP, ByteBuffer.class, Bitmap.class, byteBufferBitmapDecoder)
      .append(Registry.BUCKET_BITMAP, InputStream.class, Bitmap.class, streamBitmapDecoder)
      .append(
          Registry.BUCKET_BITMAP,
          ParcelFileDescriptor.class,
          Bitmap.class,
          parcelFileDescriptorVideoDecoder)
      .append(
          Registry.BUCKET_BITMAP,
          AssetFileDescriptor.class,
          Bitmap.class,
          VideoDecoder.asset(bitmapPool))
      .append(Bitmap.class, Bitmap.class, UnitModelLoader.Factory.getInstance())
      .append(
          Registry.BUCKET_BITMAP, Bitmap.class, Bitmap.class, new UnitBitmapDecoder())
      .append(Bitmap.class, bitmapEncoder)
      /* BitmapDrawables */
      .append(
          Registry.BUCKET_BITMAP_DRAWABLE,
          ByteBuffer.class,
          BitmapDrawable.class,
          new BitmapDrawableDecoder<>(resources, byteBufferBitmapDecoder))
      .append(
          Registry.BUCKET_BITMAP_DRAWABLE,
          InputStream.class,
          BitmapDrawable.class,
          new BitmapDrawableDecoder<>(resources, streamBitmapDecoder))
      .append(
          Registry.BUCKET_BITMAP_DRAWABLE,
          ParcelFileDescriptor.class,
          BitmapDrawable.class,
          new BitmapDrawableDecoder<>(resources, parcelFileDescriptorVideoDecoder))
      .append(BitmapDrawable.class, new BitmapDrawableEncoder(bitmapPool, bitmapEncoder))
      /* GIFs */
      .append(
          Registry.BUCKET_GIF,
          InputStream.class,
          GifDrawable.class,
          new StreamGifDecoder(imageHeaderParsers, byteBufferGifDecoder, arrayPool))
      .append(Registry.BUCKET_GIF, ByteBuffer.class, GifDrawable.class, byteBufferGifDecoder)
      .append(GifDrawable.class, new GifDrawableEncoder())
      /* GIF Frames */
      // Compilation with Gradle requires the type to be specified for UnitModelLoader here.
      .append(
          GifDecoder.class, GifDecoder.class, UnitModelLoader.Factory.getInstance())
      .append(
          Registry.BUCKET_BITMAP,
          GifDecoder.class,
          Bitmap.class,
          new GifFrameResourceDecoder(bitmapPool))
      /* Drawables */
      .append(Uri.class, Drawable.class, resourceDrawableDecoder)
      .append(
          Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitmapPool))
      /* Files */
      .register(new ByteBufferRewinder.Factory())
      .append(File.class, ByteBuffer.class, new ByteBufferFileLoader.Factory())
      .append(File.class, InputStream.class, new FileLoader.StreamFactory())
      .append(File.class, File.class, new FileDecoder())
      .append(File.class, ParcelFileDescriptor.class, new FileLoader.FileDescriptorFactory())
      // Compilation with Gradle requires the type to be specified for UnitModelLoader here.
      .append(File.class, File.class, UnitModelLoader.Factory.getInstance())
      /* Models */
      .register(new InputStreamRewinder.Factory(arrayPool))
      .append(int.class, InputStream.class, resourceLoaderStreamFactory)
      .append(
          int.class,
          ParcelFileDescriptor.class,
          resourceLoaderFileDescriptorFactory)
      .append(Integer.class, InputStream.class, resourceLoaderStreamFactory)
      .append(
          Integer.class,
          ParcelFileDescriptor.class,
          resourceLoaderFileDescriptorFactory)
      .append(Integer.class, Uri.class, resourceLoaderUriFactory)
      .append(
          int.class,
          AssetFileDescriptor.class,
          resourceLoaderAssetFileDescriptorFactory)
      .append(
          Integer.class,
          AssetFileDescriptor.class,
          resourceLoaderAssetFileDescriptorFactory)
      .append(int.class, Uri.class, resourceLoaderUriFactory)
      .append(String.class, InputStream.class, new DataUrlLoader.StreamFactory())
      .append(Uri.class, InputStream.class, new DataUrlLoader.StreamFactory())
      .append(String.class, InputStream.class, new StringLoader.StreamFactory())
      .append(String.class, ParcelFileDescriptor.class, new StringLoader.FileDescriptorFactory())
      .append(
          String.class, AssetFileDescriptor.class, new StringLoader.AssetFileDescriptorFactory())
      .append(Uri.class, InputStream.class, new HttpUriLoader.Factory())
      .append(Uri.class, InputStream.class, new AssetUriLoader.StreamFactory(context.getAssets()))
      .append(
          Uri.class,
          ParcelFileDescriptor.class,
          new AssetUriLoader.FileDescriptorFactory(context.getAssets()))
      .append(Uri.class, InputStream.class, new MediaStoreImageThumbLoader.Factory(context))
      .append(Uri.class, InputStream.class, new MediaStoreVideoThumbLoader.Factory(context))
      .append(
          Uri.class,
          InputStream.class,
          new UriLoader.StreamFactory(contentResolver))
      .append(
          Uri.class,
          ParcelFileDescriptor.class,
           new UriLoader.FileDescriptorFactory(contentResolver))
      .append(
          Uri.class,
          AssetFileDescriptor.class,
          new UriLoader.AssetFileDescriptorFactory(contentResolver))
      .append(Uri.class, InputStream.class, new UrlUriLoader.StreamFactory())
      .append(URL.class, InputStream.class, new UrlLoader.StreamFactory())
      .append(Uri.class, File.class, new MediaStoreFileLoader.Factory(context))
      .append(GlideUrl.class, InputStream.class, new HttpGlideUrlLoader.Factory())
      .append(byte[].class, ByteBuffer.class, new ByteArrayLoader.ByteBufferFactory())
      .append(byte[].class, InputStream.class, new ByteArrayLoader.StreamFactory())
      .append(Uri.class, Uri.class, UnitModelLoader.Factory.getInstance())
      .append(Drawable.class, Drawable.class, UnitModelLoader.Factory.getInstance())
      .append(Drawable.class, Drawable.class, new UnitDrawableDecoder())
      /* Transcoders */
      .register(
          Bitmap.class,
          BitmapDrawable.class,
          new BitmapDrawableTranscoder(resources))
      .register(Bitmap.class, byte[].class, bitmapBytesTranscoder)
      .register(
          Drawable.class,
          byte[].class,
          new DrawableBytesTranscoder(
              bitmapPool, bitmapBytesTranscoder, gifDrawableBytesTranscoder))
      .register(GifDrawable.class, byte[].class, gifDrawableBytesTranscoder);

  ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();
  glideContext =
      new GlideContext(
          context,
          arrayPool,
          registry,
          imageViewTargetFactory,
          defaultRequestOptions,
          defaultTransitionOptions,
          defaultRequestListeners,
          engine,
          isLoggingRequestOriginsEnabled,
          logLevel);
}

都是在这里确定的register,然而这里的register没有用到toStringUrl,都是toUrl、getCacheKey,因此我觉得新版glide已经支持ipv6了

你可能感兴趣的:(Android-app)