看一个okhttp3请求数据的例子:
OkHttpClient client = new OkHttpClient(); RequestBody body = new FormBody.Builder() .add("bookid",String.valueOf(mBook.bookid)) .add("bookName",mBookNameEdit.getText().toString()).build(); final Request request = new Request.Builder() .url("http://139.199.89.89/api/v1/books") .post(body).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.d(TAG,"<<<<e="+e); } @Override public void onResponse(Call call, Response response) throws IOException { if(response.isSuccessful()) { String d = response.body().string(); Log.d(TAG,"<<<<d="+d); } } });
post请求创建request和get是一样的,只是post请求需要提交一个表单,就是RequestBody。表单的格式有好多种,普通的表单是:
RequestBody body = new FormBody.Builder() .add("键", "值") .add("键", "值") ... .build();
RequestBody的数据格式都要指定Content-Type,常见的有三种:
application/x-www-form-urlencoded 数据是个普通表单
multipart/form-data 数据里有文件
application/json 数据是个json
但是好像以上的普通表单并没有指定Content-Type,这是因为FormBody继承了RequestBody,它已经指定了数据类型为application/x-www-form-urlencoded。
private static final MediaType CONTENT_TYPE = MediaType.parse("application/x-www-form-urlencoded");
再看看数据为其它类型的RequestBody的创建方式。
如果表单是个json:
MediaType JSON = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(JSON, "你的json");
如果数据包含文件:
RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/png"), file)) .build();
热门评论
hello,这里的表单提交的参数类型为什么都是String,如果要调用的接口的参数不是String类型,会不会报错?我测了下好像不会报错,但是为什么呢?