简体   繁体   中英

Android OkHttp addPathSegment replaces slashes

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.

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

Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM