tanay tandon tanay tandon - 1 month ago 18
Java Question

Cannot load image which has % in url with picasso

I am using Picasso v2.5.2 in my project. It works fine, however, in some cases there is a % character in the url and in such cases Picasso throws an error.

String url = "http://dev.api.getmyparking.com:5000/api/v1/Containers/gmp-booking-tickets/download/AVGA3JJ%25UAB.png";
Picasso picasso = new Picasso.Builder(this).listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
exception.printStackTrace();
}
}).build();
picasso.setLoggingEnabled(true);
picasso.load(url).placeholder(R.drawable.parking_placeholder).error(R.drawable.icn_rupee).into(mIvTicket);


The above code gives the following error:

11-07 17:07:17.383 9531-9531/com.gmpuser.app W/System.err: java.io.IOException: Invalid % sequence: %UA in path at index 91: http://dev.api.getmyparking.com:5000/api/v1/Containers/gmp-booking-tickets/download/AVGA3JJ%UAB.png
11-07 17:07:17.383 9531-10104/com.gmpuser.app D/Picasso: Dispatcher delivered [R0]+1258ms
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.android.okhttp.Request.uri(Request.java:64)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:268)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.UrlConnectionDownloader.load(UrlConnectionDownloader.java:93)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.NetworkRequestHandler.load(NetworkRequestHandler.java:47)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:206)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at java.lang.Thread.run(Thread.java:818)
11-07 17:07:17.393 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)


Encoding the URL seems to be a solution, however, I encoded the url using Uri.encode and then supplied the encoded string to Picasso but that does not work as well.

String url = "http://dev.api.getmyparking.com:5000/api/v1/Containers/gmp-booking-tickets/download/AVGA3JJ%25UAB.png";
String urlEncoded = Uri.encode(url);
Picasso picasso = new Picasso.Builder(this).listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
exception.printStackTrace();
}
}).build();
picasso.setLoggingEnabled(true);
picasso.load(urlEncoded).placeholder(R.drawable.parking_placeholder).error(R.drawable.icn_rupee).into(mIvTicket);


The above code gives the following error

11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: java.lang.IllegalStateException: Unrecognized type of request: Request{http%3A%2F%2Fdev.api.getmyparking.com%3A5000%2Fapi%2Fv1%2FContainers%2Fgmp-booking-tickets%2Fdownload%2FAVGA3JJ%25UAB.png}
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.BitmapHunter$2.load(BitmapHunter.java:66)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:206)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:159)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at java.lang.Thread.run(Thread.java:818)
11-07 17:09:42.313 9531-9531/com.gmpuser.app W/System.err: at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:411)

Answer

Try this,

String urlStr = "http://dev.api.getmyparking.com:5000/api/v1/Containers/gmp-booking-tickets/download/AVGA3JJ%25UAB.png";
String url = Uri.parse(urlStr)
                .buildUpon()
                .build()
                .toString();

See my answer here.