Okhttp3的常用方法和简单使用

多的就不介绍了,直接进入正题

本文依赖:

//OkHttp3
implementation 'com.facebook.stetho:stetho-okhttp3:1.5.0'
//打印okHttp3日志
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'

需要注意的几点

一、结果返回后的线程不是主线程

二、成功后的结果需要用Response对象response.body().string()。注:其中response.body()可能是null需要注意

1.同步get请求:

public String getExecuteHttp() throws IOException {
    //1.创建OkHttpClient对象
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder()
            .url("http://www.baidu.com")
            .build();
    Response response = okHttpClient.newCall(request).execute();
    return response.body().string();
}

2.异步get请求:

 

   public void getEnqueueHttp() {
        //1.创建OkHttpClient对象
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
//                .get()                 这两个方法效果一样,也可以都不加,默认get请求
//                .method("GET",null)
                .url("http://www.baidu.com")
                .build();
        okHttpClient.newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("onFailure", "onFailure");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.d("onResponse", "onResponse" + response.body().string());
                    }
                });
    }

 3.异步post请求:

public void postHttp() {
    OkHttpClient okHttpClient = new OkHttpClient();
    RequestBody requestBody = new FormBody.Builder()
            .add("page", "1")
            .add("count", "10")
            .build();
    Request request = new Request.Builder()
            .url("http://www.duans.top/freeApi/api.php?act=getJoke")
            .post(requestBody)
            .build();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.d("onFailure", "onFailure");
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            Log.d("onResponse", "onResponse" + response.body().string());
        }
    });
}

4.设置超时:

//1.创建OkHttpClient对象
OkHttpClient okHttpClient = new OkHttpClient.Builder()
        //失败重试,但不能指定次数
        .retryOnConnectionFailure(true)
        //单位可以自行尝试,此处单位秒
        .connectTimeout(15, TimeUnit.SECONDS)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(30, TimeUnit.SECONDS)
        .build();

5.打印请求的log日志 

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
//设置等级BODY表示打印全部,也可以更改打印内容
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

使用方式:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .build();

也可以自定义打印内容:

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
    @Override
    public void log(String message) {
        Log.e("okHttp->", message);
    }
});

不断进步 

你可能感兴趣的:(android)