okhttp基本使用
FormBody
用来提交一些表单数据。通过FormBody.Builder来添加表单数据。如下所示:
OkHttpClient client = new OkHttpClient();
FormBody formBody = new FormBody.Builder()
.add("username", "jason")
.add("password", "magicer")
.build();
Request request = new Request.Builder()
.post(formBody)
.addHeader("User-Agent", "Apple")
.build();
Response response = client.newCall(request).execute();
在FromBody中,查看其源码我们可以看到FormBody设置的Content-Type为"application/x-www-form-urlencoded".也就是普通表单数据。
private static final MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");