user2219372 user2219372 - 4 months ago 11
Java Question

How to use OKHTTP to make a post request?

I read some examples which are posting jsons to the server.

some one says :


OkHttp is an implementation of the HttpUrlConnection interface
provided by Java. It provides an input stream for writing content and
doesn't know (or care) about what format that content is.


Now I want to make a normal post to the URL with params of name and password.

It means I need to do encode the name and value pair into stream by myself?

Answer

You need to encode it yourself by escaping strings with URLEncoder and joining them with "=" and "&". Or you can use FormEncoder from Mimecraft which gives you a handy builder.

FormEncoding fe = new FormEncoding.Builder()
    .add("name", "Lorem Ipsum")
    .add("occupation", "Filler Text")
    .build();
Comments