ITPub博客

首页 > IT职业 > IT图书 > okhttp3第一篇

okhttp3第一篇

IT图书 作者:wzzhujun 时间:2021-09-09 09:49:26 0 删除 编辑

1.首先http://square.github.io/okhttp/这是官网地址

2.最简单的调用,同步跟异步调用,

public static void sendHttps(String url){
    OkHttpClient client=new OkHttpClient();
    Request request=new Request.Builder().url(url).build();
    try {
        Response response=client.newCall(request).execute();
        if (response.isSuccessful()){
            System.out.println(response.body().string());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void sendAsync(String url){
 OkHttpClient client=new OkHttpClient();
    Request request=new Request.Builder().url(url).build();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()){
                System.out.println(response.body().string());
            }
        }
    });
}
可以用了,第二篇介绍请求头介绍

原文链接:http://www.apkbus.com/blog-340477-76634.html

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3370/viewspace-2813019/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    83
  • 访问量
    732