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?
As per the docs, OkHttp version 3 replaced FormEncodingBuilder
with FormBody
and FormBody.Builder()
, so the old examples won't work anymore.
Form and Multipart bodies are now modeled. We've replaced the opaque
FormEncodingBuilder
with the more powerfulFormBody
andFormBody.Builder
combo.Similarly we've upgraded
MultipartBuilder
intoMultipartBody
,MultipartBody.Part
, andMultipartBody.Builder
.
So if you're using OkHttp 3.x try the following example:
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("message", "Your message")
.build();
Request request = new Request.Builder()
.url("http://www.foo.bar/index.php")
.post(formBody)
.build();
try {
Response response = client.newCall(request).execute();
// Do something with the response.
} catch (IOException e) {
e.printStackTrace();
}