【问题标题】:android - Picasso with OkHTTP can not load images containing vertical bar character ( "|" ) in their URLandroid - 带有 OkHTTP 的 Picasso 无法加载其 URL 中包含竖线字符(“|”)的图像
【发布时间】:2015-11-26 18:23:59
【问题描述】:

我在我的项目中使用 Picasso 和 MapBox。只要 MapBox 的依赖项中有 OkHTTP,我就不得不使用 OkHTTP。 但是当我将 OkHTTP 添加到 gradle 依赖项时,Picasso 无法加载其 URL 中包含竖线字符(“|”)的图像。

没有 OkHTTP:

Picasso.with(context).load("http://example.com/image.jpg").into(imageView); //OK Picasso.with(context).load("http://example.com/image.jpg|100:100").into(imageView); //OK

使用 OkHTTP:

Picasso.with(context).load("http://example.com/image.jpg").into(imageView); //09-01 19:07:35.280 24916-24916/com.test D/Picasso﹕ Main errored [R6]+287ms

Picasso.with(context).load("http://example.com/image.jpg|100:100").into(imageView); //OK

所以,我的问题是:如果另一个库需要 OkHTTP,如何避免在 Picasso 中使用 OkHTTP,或者如何解决这个问题并继续使用 OkHTTP?

【问题讨论】:

    标签: gradle mapbox picasso okhttp


    【解决方案1】:

    试试 OkHttp 2.5.0。它支持包含| 字符的URL。

    【讨论】:

      【解决方案2】:

      我已经通过在MyApplication.onCreate()方法中添加下一个代码解决了这个问题:

          Picasso picasso = new Picasso.Builder(getApplicationContext())
                  .requestTransformer(new Picasso.RequestTransformer() {
                      @Override
                      public Request transformRequest(Request request) {
                          Uri oldImageUri = request.uri;
                          Uri newImageUri = oldImageUri.buildUpon().query(oldImageUri.getEncodedQuery()).build();
                          return request.buildUpon().setUri(newImageUri).build();
                      }
                  }).build();
          picasso.setLoggingEnabled(true);
          Picasso.setSingletonInstance(picasso);
      

      【讨论】:

        【解决方案3】:

        当我开始使用 OkHttp 时,我遇到了同样的问题。问题似乎是必须对竖线字符进行编码。只需用编码值 (%7C) 替换它,它应该可以正常工作。在您的情况下,网址变为:

        http://example.com/image.jpg%7C100:100
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-18
          • 2019-08-14
          • 2019-08-20
          • 1970-01-01
          • 2018-07-14
          • 2020-03-18
          相关资源
          最近更新 更多