0

I am new to Android Development and I would like to know how to perform a GET request using okhttp. I have referred http://square.github.io/okhttp/, but they only have examples of POST request. I have tried this -

        okHttpClientLogin = new OkHttpClient();
        requestBodyLogin = new FormBody.Builder()
                .addEncoded("name", name_input) // params
                .addEncoded("keys", keys_input) //params
                .build();

        requestLogin = new Request.Builder()
                .addHeader("Authorization", token_type + " " +access_token)
                .url(LOGIN_URL)
                .get()
                .build();

and got an Error :

{"status":{"status":206,"msg":"No record found"},"user":null}

I know why this error is coming, because the params have not been entered. I also tried passing requestBodyLogin inside .get() but it's not allowing.

hazardous
  • 10,627
  • 2
  • 40
  • 52
  • "Please give a Solution for this problem." You may have not meant it this way, but this sounds bad in English, like a boss talking to his employee. You might want to rephrase or remove that line. – GAntoine Feb 06 '16 at 06:33
  • 1
    From the error it appears that the Get call in working, however your service end-point is throwing an application error. Add details like what parameters are required by the end-point. – hazardous Feb 06 '16 at 07:38
  • For okhttp3, here is a solution: https://stackoverflow.com/a/43029045/1784529 – Yun CHEN Sep 14 '17 at 11:58

1 Answers1

1

Since OkHTTP 2.4, there's the function addQueryParameter. You can either use a HttpUrl, a String or a java.net.URL as url. Basically, just create a new HttpUrl.Builder() and use the function addQueryParameter.

Example taken from the javadocs:

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

http://square.github.io/okhttp/3.x/okhttp/okhttp3/HttpUrl.html

http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/HttpUrl.Builder.html#addQueryParameter-java.lang.String-java.lang.String-

jossiwolf
  • 1,865
  • 14
  • 22