0

What I want to achieve ?

I am trying to send two parameters in my Server URL using OkHttp through both get and post bcoz i want to know the syntax for both the methods.

What I had Tried ?

I have searched SO for questions on OkHttp but those had not solved my IllegalArgumentException.

I have seen below links :

Add query params to a GET request in okhttp in Android

and this

How to add parameters to api (http post) using okhttp library in Android

and

How to add query parameters to a HTTP GET request by OkHttp?

Exception from code 2:

enter image description here

Code I had used till now :

1)GET

   urls = chain.request().httpUrl() <-- NullPointerException Line
                    .newBuilder()
                    .scheme("http")
                    .host(SERVER_IP)
                    .addQueryParameter("from", valueFrom)
                    .addQueryParameter("to",valueTo)
                    .build();

        request = chain.request().newBuilder().url(urls).build();

        response = chain.proceed(request);

2)GET

     urls =new HttpUrl.Builder()
                    .host(SERVER_IP)  <--- IllegalArgumentException line
                    .addQueryParameter("from", valueFrom)
                    .addQueryParameter("to", valueTo)
                    .build();

            request = new Request.Builder().url(urls).build(); 

            response = client.newCall(request).execute();  

3)POST

  body = new      
  MultipartBuilder().type(MultipartBuilder.FORM).addFormDataPart("from",
  valueFrom).addFormDataPart("to",valueTo).build();

        Log.i("Body data",""+body.toString());

        request = new Request.Builder().url(params[0]).post(body).build();

        Log.i("Request data",""+request.toString());

        response = client.newCall(request).execute();

4)POST

  body = new FormEncodingBuilder().add("from", valueFrom).add("to", 
  valueTo).build();

        Log.i("Body data",""+body.toString());

        request = new Request.Builder().url(params[0]).post(body).build();

        Log.i("Request data",""+request.toString());

        response = client.newCall(request).execute();

build.gradle

  dependencies 
  {
   compile files('libs/okhttp-2.5.0.jar')
   compile files('libs/okio-1.6.0.jar')
  }

Thanks In Advance...

Edit :

The above code for POST request is working fine now

But for GET request I still have no solution.

1 Answer 1

-2

Just set your GET parameter extend your URL:

RequestBody body = new FormEncodingBuilder()
                .add("requestParamName", requestParameter.getRequestParams())
                .build();


        Request request = new Request.Builder()
                .url("https://www.test.com/serviceTest?parama=abc&paramb=123")
                .post(body)
                .build();
Sign up to request clarification or add additional context in comments.

1 Comment

Are you sure it is a GET request call ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.