Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android OkHttp addPathSegment replaces slashes

Tags:

android

okhttp

I am using OkHttp 2.4.0.

HttpUrl url = new HttpUrl.Builder()
            .scheme("https")
            .host("www.something.com")
            .addPathSegment("/api/v1/doc")
            .build();

The expected url is: https://www.something.com/api/v1/doc

What I get is: https://www.something.com%2Fapi%2Fv1%2Fdoc

The "/" in the pathSegment are replaced with "%2F". Why does this occur and how can it be avoided since i get an invalid Url exception because apache does not allow "%2F" in a url.

like image 439
JY2k Avatar asked Jul 13 '15 13:07

JY2k


3 Answers

Try this:

    HttpUrl url = new HttpUrl.Builder()
        .scheme("https")
        .host("www.something.com")
        .addPathSegment("api")
        .addPathSegment("v1")
        .addPathSegment("doc")
        .build();
like image 157
dors Avatar answered Oct 19 '22 11:10

dors


This solution is a little bit more elegant, and OkHttp doesn't replace slashes in this case :)

HttpUrl url = new HttpUrl.Builder()
    .scheme("https")
    .host("www.something.com")
    .addPathSegments("api/v1/doc")
    .build();
like image 20
PAD Avatar answered Oct 19 '22 11:10

PAD


Delete the slashes and concatenate the segments like this:

HttpUrl url=new HttpUrl.Builder()
    .scheme("https")
    .host("www.something.com")
    .addPathSegment("api")
    .addPathSegment("v1")
    .addPathSegment("doc")
    .build();
like image 3
isma3l Avatar answered Oct 19 '22 11:10

isma3l