Bebbolin
Bebbolin

Reputation: 101

Add queryparam OKHttp3

i'm making a GET request to my API and the cURL looks like:

curl -X GET "http://www.example.com/api/users?role=ROLE1&role=ROLE2&role=ROLE3&pag=0&num=0" -H "accept: application/json"

I try to make the request as follow from my Android Application, but the "addQueryParameter" doesn't appending the role

public void getActors(String[] roles, final ApiManager.Callback<List<UsersDetails>> callbackUsers) throws JSONException{
      final String url = BuildConfig.BASE_AUTH_URL + "/users";

      HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder();
      httpBuilder.addQueryParameter("roles", "ROLE1");
      httpBuilder.addQueryParameter("roles", "ROLE2");
      httpBuilder.addQueryParameter("roles", "ROLE3");
      httpBuilder.addQueryParameter("pag", "0");
      httpBuilder.addQueryParameter("num", "0");


      Request request = new Request.Builder()
              .url(httpBuilder.build())
              .addHeader("Content-type", "application/json")
              .get()
              .build();
...

EDIT: the code with this change works, but i'm getting a null from server.

The roles will be always the same in each GET. Can someone please help me understand where i'm making error?

Upvotes: 1

Views: 379

Answers (1)

Rahul Khurana
Rahul Khurana

Reputation: 8835

You can give try to okHttpBuilder and as many as you want.

FormBody.Builder builder = new FormBody.Builder();
for (String m : list) {
    builder.add("roles", m);
}

and then use it like this

url.post(body.build());

or

httpBuilder.addQueryParameter("roles"+[0], "ROLE1");
httpBuilder.addQueryParameter("roles"+[1], "ROLE1");

Upvotes: 1

Related Questions