0

Please help me to encode the url:

http://dsxsd.com/def/fss/default/files/10 رسم الصباح - 1.mp3

I need to download this file in android app. when i am trying to make url connection it gives me 404. but on browser the url is working. when i pasted browser encoded url its working fine.

But i can't hardcore the things as i have my urls like this coming from server.

I tried to encode using Urlencoder.encode(url,"UTF-8"); but server call gives me wrong url I am getting "java.net.MalformedURLException: Protocol not found: http%3A%2F%........

have this url not separate path params and query params to encode separately.

please help me in it.

3 Answers 3

1

Use URI templates: URLEncoder.encode() does not work.

Usage of the library above:

final URITemplate template = new URITemplate("http://dsxsd.com/def/" +
    "fss/default/files/{filename}");

final VariableMap vars = VariableMap.newBuilder()
    .addScalarValue("filename", "10 رسم الصباح - 1.mp3")
    .freeze();

System.out.println(template.toString(vars));

Output:

http://dsxsd.com/def/fss/default/files/10%20%D8%B1%D8%B3%D9%85%20%D8%A7%D9%84%D8%B5%D8%A8%D8%A7%D8%AD%20-%201.mp3
Sign up to request clarification or add additional context in comments.

8 Comments

For this particular example, your library doesn't achieve anything that you cannot achieve with URLEncoder.
agree but, and library looks promising . can't i get a simple project other than Gradle build. its hard for person who don't use Gradle to configure it.
@Rajivyadav why clone the project? Just use the gradle/maven artifact
sorry i am getting . Error generating final archive: Found duplicate file for APK: ASL-2.0.txt Origin 1: /home/user/.gradle/caches/modules-2/files-2.1/com.github.fge/jackson-coreutils/1.5/5108553833b399fed6a1f7f979b89a07b786d3c2/jackson-coreutils-1.5.jar Origin 2: /home/user/.gradle/caches/modules-2/files-2.1/com.github.fge/btf/1.1/597aaacc063dcf8b77c17729706b895c562b162e/btf-1.1.jar
@Rajivyadav that looks like a bug... Could you open a bug in github? In particular, describe your build environment (what is APK for instance?)
|
1

The problem is that you're encoding the entire URL, you only need to encode the filename, e.g.

String filename = Uri.encode("10 رسم الصباح - 1.mp3");
String url = "http://dsxsd.com/def/fss/default/files/" + filename;

5 Comments

I am getting this url from server. I am not configuring it locally . BTW i tried to sub string it and encoding it it is returing dsxsd.com/def/fss/default/files/… which is not a right url.
In that case, you could split the URL and just encode the last part of that path.
that is not helping me out. spaces creates problem .
@Philio no it won't... "+" is not a valid regex... You probably mean .replace(), and even then the set of characters URLEncoder encodes is not the same set as required by a URI path segment; see the README of the project I linked to for more details...
True, it would need to be escaped. However, Uri.encode is a better solution anyway as it correctly encodes spaces. Solution updated.
0

If you only have the URL, use this method

private String encodeUrl(String link) throws UnsupportedEncodingException {
    Uri uri = (Uri.parse(link));
    String result = null;
    if (Objects.equals(uri.getScheme(), "content")) {
        try (Cursor cursor = getContentResolver().query(uri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = Objects.requireNonNull(result).lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return link.replace(result
            ,URLEncoder.encode(result, "UTF-8")
                    .replace("+", "%20"));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.