Add query params to a GET request in okhttp in Android

后端 未结 4 833
星月不相逢
星月不相逢 2021-01-12 00:56

Is there a way to add query params (?param1=val1¶m2=val2) to a GET request using okhttp in Android?

I am looking for an API and no

4条回答
  •  Happy的楠姐
    2021-01-12 01:29

    Try HttpUrl class (in okhttp package).


    //adds the pre-encoded query parameter to this URL's query string
    addEncodedQueryParameter(String encodedName, String encodedValue)
    
    //encodes the query parameter using UTF-8 and adds it to this URL's query string
    addQueryParameter(String name, String value)
    

    Note: if there are already name/value pairs with this name, these functions will just add another pair


    setEncodedQueryParameter(String encodedName, String encodedValue)
    
    setQueryParameter(String name, String value)
    

    Note: if there are already name/value pairs with this name, these functions will remove them and only after that add this new pair


    Example:

    HttpUrl url = new HttpUrl.Builder()
        .scheme("https")
        .host("www.google.com")
        .addPathSegment("search")
        .addQueryParameter("q", "polar bears")
        .build();
    

提交回复
热议问题