OKHTTP3的简单使用,表单提交,json传递

OKHTTP3的简单使用

使用okhttp3需引入以下依赖

 compile 'com.squareup.okio:okio:1.13.0'
 compile 'com.squareup.okhttp3:okhttp:3.9.0'

1.okhttp GET方式请求


            //创建OkHttpClient对象
            OkHttpClient okHttpClient  = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10,TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS)
                    .build();

            final Request request = new Request.Builder()
                    .url("http://172.20.192.168:8080/getbookByFrom?name=android基础&price=50")//请求的url
                    .get()//设置请求方式,get()/post()  查看Builder()方法知,在构建时默认设置请求方式为GET
                    .build(); //构建一个请求Request对象

            //创建/Call
            Call call = okHttpClient.newCall(request);
            //加入队列 异步操作
            call.enqueue(new Callback() {
                //请求错误回调方法
                @Override
                public void onFailure(Call call, IOException e) {
                    System.out.println("连接失败");
                }
                //异步请求(非主线程)
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.code()==200) {
                        System.out.println(response.body().string());
                    }             
            });

注:在okhttp3中 如果想设置超时时间直接通过OkHttpClient 对象来设置

     okHttpClient .setConnectTimeout(10, TimeUnit.SECONDS);  
     okHttpClient .setWriteTimeout(10, TimeUnit.SECONDS);  
     okHttpClient .setReadTimeout(20, TimeUnit.SECONDS); 

但okhttp3OkHttpClient 对象没有这个方法,需要借助new OkHttpClient.Builder()按照上面的方法设置超时时间。

2.okhttp POST方式请求(提交表单方式)

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("name", "android基础")
                .add("price", "50")
                .build();

        final Request request = new Request.Builder()
                .url("http://172.20.192.168:8080/getbookByFrom")//请求的url
                .post(formBody)
                .build();


        //创建/Call
        Call call = okHttpClient.newCall(request);
        //加入队列 异步操作
        call.enqueue(new Callback() {
            //请求错误回调方法
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("连接失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code()==200) {
                    System.out.println(response.body().string());
                }
            }
        });

3.okhttp POST方式请求(提交json方式)

 OkHttpClient okHttpClient  = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();

        Book book = new Book();
        book.setName("android基础");
        book.setPrice(59);
        //使用Gson 添加 依赖 compile 'com.google.code.gson:gson:2.8.1'
        Gson gson = new Gson();
        //使用Gson将对象转换为json字符串
        String json = gson.toJson(book);

        //MediaType  设置Content-Type 标头中包含的媒体类型值
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
        , json);

         Request request = new Request.Builder()
                .url("http://172.20.192.168:8080/getbookByJson")//请求的url
                .post(requestBody)
                .build();

        //创建/Call
        Call call = okHttpClient.newCall(request);
        //加入队列 异步操作
        call.enqueue(new Callback() {
            //请求错误回调方法
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("连接失败");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(response.body().string());
            }
        });

你可能感兴趣的:(框架)