Search code examples
gradlemapboxpicassookhttp

android - Picasso with OkHTTP can not load images containing vertical bar character ( "|" ) in their URL


I use Picasso and MapBox in my project. As long as MapBox has OkHTTP in it's dependecies, I'm forced to use OkHTTP. But when I add OkHTTP to gradle dependencies, Picasso becomes unable to load images containing vertical bar character ( "|" ) in their URL.

Without 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

With 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

So, my question is: How to avoid using OkHTTP with Picasso if another library needs OkHTTP or how to resolve this problem and continue using OkHTTP?


Solution

  • I have resolved the problem by adding the next code in MyApplication.onCreate() method:

        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);