OKhttp post请求

//创建OkHttpClient对象
    OkHttpClient client = new OkHttpClient();
//通过FormEncodingBuilder对象添加多个请求参数键值对
    FormEncodingBuilder builder = new FormEncodingBuilder();
    builder.add("loginName", "xxx").add("password", "xxx");
//通过FormEncodingBuilder对象构造Post请求体存放键值对
    RequestBody body = builder.build();
//通过请求地址和请求体构造Post请求对象Request
    Request request = new Request.Builder().url(url).post(body).build();

    Call call = client.newCall(request);
    call.enqueue(new com.squareup.okhttp.Callback() {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {
            if (response.isSuccessful()) {
                String result = response.body().string();
                Log.e("result",result);
            } else {
                throw new IOException("Unexpected code " + response);
            }
        }
    });

你可能感兴趣的:(OKhttp post请求)