OkHttp3的使用

来源:互联网 发布:linux口令长度 编辑:程序博客网 时间:2024/05/17 20:22

一、首先需要Gradle,GitHub的链接:http://square.github.io/okhttp/

compile 'com.squareup.okhttp3:okhttp:3.9.0'
二、测试get方法

/** * 测试get方法 */@Testpublic void testGet() {    //创建OKHttpClient对象    OkHttpClient okHttpClient = new OkHttpClient();    //创建request对象    Request request = new Request.Builder()            .url("http://httpbin.org/get?id=id")            .build();    try {        Response response = okHttpClient.newCall(request).execute();        System.out.print(response.body().string());    } catch (IOException e) {        e.printStackTrace();    }}
三、测试post方法

/** * 测试post方法 */@Testpublic void testPost() {    //创建OKHttpClient对象    OkHttpClient okHttpClient = new OkHttpClient();    //创建request对象    MediaType mediaType = MediaType.parse("application/json;charset=utf-8");    RequestBody body = RequestBody.create(mediaType, "{\"name\",\"东哥\"}");    Request request = new Request.Builder()            .url("http://httpbin.org/post")            .post(body)            .build();    try {        Response response = okHttpClient.newCall(request).execute();        System.out.print(response.body().string());    } catch (IOException e) {        e.printStackTrace();    }}
四、测试拦截器

/** * 测试拦截器 */@Testpublic void testInterceptor() {    //定义一个拦截器    Interceptor interceptor = new Interceptor() {        @Override        public Response intercept(Chain chain) throws IOException {            long start = System.currentTimeMillis();            Request request = chain.request();            Response response = chain.proceed(request);//继续向下传递            long end = System.currentTimeMillis();            System.out.print("cost time:" + (end - start));            return response;        }    };    //创建OKHttpClient对象    OkHttpClient okHttpClient = new OkHttpClient            .Builder()            .addInterceptor(interceptor)            .build();    //创建request对象    Request request = new Request.Builder()            .url("http://httpbin.org/get?id=id")            .build();    try {        Response response = okHttpClient.newCall(request).execute();        System.out.print(response.body().string());    } catch (IOException e) {        e.printStackTrace();    }}
五、测试缓存

/** * 测试缓存 */@Testpublic void testCache() {    //创建缓存对象    Cache cache = new Cache(new File("cache.cache"), 1024 * 1024);    //创建OKHttpClient对象    OkHttpClient okHttpClient = new OkHttpClient.Builder().cache(cache).build();    //创建request对象    Request request = new Request.Builder()            .url("http://httpbin.org/get?id=id")            //.cacheControl(CacheControl.FORCE_CACHE)强制为缓存            .build();    try {        Response response = okHttpClient.newCall(request).execute();        Response cacheResponse = response.cacheResponse();        Response networkResponse = response.networkResponse();        if (cacheResponse != null) {            System.out.print("来自缓存");        } else if (networkResponse != null) {            System.out.print("来自网络");        }        System.out.print(response.body().string());    } catch (IOException e) {        e.printStackTrace();    }}

原创粉丝点击