Delphian
Delphian

Reputation: 1760

Retrofit2 complex request

I am trying to create a request using Retrofit2. I created the request using a standard library:

path = "https://www.iii.com/?id="+id+"&data=";
query = "{\"name\":\""+name+"\",\"quantity\":20}";

Final link is:

link = path+URLEncoder.encode(query, "UTF-8");

I tried different Retrofit2 options, but I can't understand how to translate my link to Retrofit2 link using together path and query with url encoded?

Upvotes: 0

Views: 359

Answers (2)

KuLdip PaTel
KuLdip PaTel

Reputation: 1069

you can add anotation for that like below

@Headers("charset=UTF-8")
@GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
        @Query("id") int id,
        @Query("data") String data
);

Upvotes: 1

Tenten Ponce
Tenten Ponce

Reputation: 2506

You can parse GET query parameters to retrofit using this code:

@GET("https://www.iii.com")
Observable<ResponseBody> getSomething(
        @Query("id") int id,
        @Query("data") String data
);

Retrofit will build it for you. Just pass your variables (assuming you know how to call retrofit requests) and retrofit will url encode it for you. You can refer to this link: https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html

Values are converted to strings using Retrofit.stringConverter(Type, Annotation[]) (or Object.toString(), if no matching string converter is installed) and then URL encoded. null values are ignored. Passing a List or array will result in a query parameter for each non-null item.

Upvotes: 1

Related Questions