How to Add Parameters to API (Http Post) Using Okhttp Library in Android

How to use OKHTTP to make a post request?

The current accepted answer is out of date. Now if you want to create a post request and add parameters to it you should user MultipartBody.Builder as Mime Craft now is deprecated.

RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("somParam", "someValue")
.build();

Request request = new Request.Builder()
.url(BASE_URL + route)
.post(requestBody)
.build();

how to pass web api(post request) url parameters as requestbody in android using okhttp

This is the solution:

String loginAPI = "http://api.myapi.com/api/authentication?email="+mEmail.toLowerCase()+"&password="+mPassword;

RequestBody reqbody = RequestBody.create(null, new byte[0]);
Request req = new Request.Builder()
.url(loginAPI)
.method("POST", reqbody)
.build();

Response response = client.newCall(req).execute();

OkHTTP (v3.0.0-RC1) Post Request with Parameters

Yes, you are adding the query parameters incorrectly. Here's how it should be done:

final OkHttpClient client = new OkHttpClient();

HttpUrl url = HttpUrl.parse("https://myUrl.com/login").newBuilder()
.addQueryParameter("password", "123456")
.addQueryParameter("username", "123456")
.build();

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

The problem is that you are submitting your data in the body of the request, as if it were a HTML form submit, and not as a query parameter, as you intended. Using HttpUrl allows you to add query parameters to your URL.

Worth noting that you can also simply do this:

Request request = new Request.Builder()
.url("https://myurl.com/login?username=123456&password=12345")
.build();

So:

  • Use HttpUrl and it's builder to create your URL and add parameters.

  • Use FormBody to create the content of your request (as if it were a html form you're submitting), if you need it.

Also, make sure you have internet permission in your app, make sure you have an active internet connection, etc, but it seems you already have.

Note: this is for OkHttp 2.x (since that's what I use), but it should be the same or similar for 3.x.

Let me know if it works for you.

How do I post data using okhttp library with content type x-www-form-urlencoded?

You select MediaType MultipartBuilder.FORM
which is for uploading the file/image as multipart

public static final MediaType FORM = MediaType.parse("multipart/form-data");

try to send like this as

private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
RequestBody formBody = new FormBody.Builder().add("search", "Jurassic Park").build();
Request request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(formBody).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
}

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

As mentioned in the other answer, okhttp v2.4 offers new functionality that does make this possible.

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



This is not possible with the current version of okhttp, there is no method provided that will handle this for you.

The next best thing is building an url string or an URL object (found in java.net.URL) with the query included yourself, and pass that to the request builder of okhttp.

Sample Image

As you can see, the Request.Builder can take either a String or an URL.

Examples on how to build an url can be found at What is the idiomatic way to compose a URL or URI in Java?

Post a request in java

I have solved this question.

 Thread thread = new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder().add("keyword", "android").build();
Request request = new Request.Builder()
.url("http://gankio.herokuapp.com/search")
.post(requestBody)
.build();
try {
Response response = client.newCall(request).execute();
Log.d("TAG", response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();


Related Topics



Leave a reply



Submit