postFormSubmit(name, pwd);break;caseR.id.btn_download: downLoadFile();break; } }/*** Post异步提交:登录 * *@paramaccount *@parampassword*/privatevoidpostSubmit(String account, String password) { String url= "http://192.168.10.24:8881/Api/GetLoginInfo.ashx?"; BaseOkHttpClient.newBuilder()...
post 请求 -- form表单提交 OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) .readTimeout(20, TimeUnit.SECONDS) .build(); //post方式提交的数据 FormBody formBody = new FormBody.Builder() .add("t", "test"...
post请求创建request和get是一样的,只是post请求需要提交一个表单,就是RequestBody。表单的格式有好多种,普通的表单是: RequestBody body = new FormBody.Builder() .add("键", "值") .add("键", "值") ... .build(); RequestBody的数据格式都要指定Content-Type,常见的有三种: application/x-www-form...
void onSuccessful(Call call, String data); void onFailure(Call call, String errorMsg); } } 调用代码: public static void main(String[] args) { // get请求,方法顺序按照这种方式,切记选择post/get一定要放在倒数第二,同步或者异步倒数第一,才会正确执行 OkHttpUtils.builder().url("请求地址,http/htt...
三丶POST请求 packagecom.lxl.learn.okhttp3;importokhttp3.Call;importokhttp3.FormBody;importokhttp3.OkHttpClient;importokhttp3.Request;importokhttp3.RequestBody;importokhttp3.Response;publicclassLearn_Okhttp3 {publicstaticvoidmain(String[] args)throwsException { ...
Post请求 同步异步都与get方法一致,后面提供异步栗子: publicvoidpost(){Stringdata="{\"username\" : \"admin\", \"password\" : \"12345\"}";RequestBodyrequestBody=RequestBody.create(JSON,data);Requestrequest=newRequest.Builder().url("http://200.200.200.182:9999/login").post(requestBody).build...
因为所有数据都是以键值对的表单形式提交,所以要设置setType(MultipartBody.FORM)。 请求抓包结果: 可见请求体重确实包含了姓名、电话、头像,并且注意到Content-Type值是 multipart/form-data。响应是200,说明请求成功了。 其他请求方式像put、header、delete,主要在构建Request时把get()或post()换成put()、header()...
Post请求 String url = "https://www.baidu.com/"; OkHttpClient okHttpClient = new OkHttpClient(); RequestBody body = new FormBody.Builder() .add("键", "值") .add("键", "值") ... .build(); Request request = new Request.Builder() .url(url) .post(body) .build(); Call call...
(key, value);}//构建formBody,将其传入Request请求中FormBody body = builder.build();Request request = new Request.Builder().url(url).post(body).build();Call call = client.newCall(request);//返回请求结果try (Response response = call.execute()) {return response.body().string();} catch ...
FormBody.BuilderformBodyBuilder=newFormBody.Builder();formBodyBuilder.add("username","john");formBodyBuilder.add("password","123456");RequestBodyrequestBody=formBodyBuilder.build(); 1. 2. 3. 4. 创建Request 创建一个POST请求的Request对象,并将之前创建的RequestBody传入: ...