joseph joseph - 3 months ago 9
Android Question

How to Create non-Standard or Malformed Java URL object deliberately

I am trying to use the Glide Android library to load images from urls such as

String urlString = "https://images.example.com/is/image/example/EXMPL1234_021347_1?$cdp_thumb$";


When I enter the above url string into Glide,

Glide.with(mContext).load(urlString).into(view);


the address it actually hits is:

https://images.example.com/is/image/example/EXMPL1234_021347_1?%24cdp_thumb%24


which is for me incorrect. The $ must be preserved.

I tried then to pass in java.net.URL object in, but I again seem unable to construct one that preserves the $ character inside the query.

Here is an example of something I tried:

URL url = null;
try
{
url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath()+"?$hp_wn_thumb_app$", "", url
.getRef()
+"?$hp_wn_thumb_app$");
url = uri.toURL();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (URISyntaxException e)
{
e.printStackTrace();
}


I would be very grateful if someone could suggest a solution. to making non-urlencoded java URL objects.

Answer

This solves this original problem.

package com.techventus;

import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.Headers;

import java.net.MalformedURLException;
import java.net.URL;

/*
// Necessary to help make correct calls to the API which does not apply and //URL special Character correction.
*/
public class CorrectGlideUrl extends GlideUrl
{
    String urlString;

    public CorrectGlideUrl(URL url)
    {
        super(url);
    }

    public CorrectGlideUrl(String url)
    {
        super(url);
        urlString = url;
    }

    public CorrectGlideUrl(URL url, Headers headers)
    {
        super(url, headers);
    }

    public CorrectGlideUrl(String url, Headers headers)
    {
        super(url, headers);
    }

    /**
     *
     * Returns a properly escaped {@link String} url that can be used to make http/https requests.
     *
     * @see #toURL()
     * @see #getCacheKey()
     */
    @Override
    public String toStringUrl() {

        return urlString.replace("%24","$");
//      return getSafeStringUrl();
    }


    /**
     * Returns a properly escaped {@link java.net.URL} that can be used to make http/https requests.
     *
     * @see #toStringUrl()
     * @see #getCacheKey()
     * @throws MalformedURLException
     */
    @Override
    public URL toURL() throws MalformedURLException {
        return getSafeUrl();
    }

    // See http://stackoverflow.com/questions/3286067/url-encoding-in-android. Although the answer using URI would work,
    // using it would require both decoding and encoding each string which is more complicated, slower and generates
    // more objects than the solution below. See also issue #133.
    private URL getSafeUrl() throws MalformedURLException {

        return null;
    }


}
Comments