在使用OkHttp之前,需要在项目中添加OkHttp库的依赖。

1、在编辑app/build.gradle的文件,添加下述依赖库,会自动下载两个库,一个是OkHttp库,一个是Okio库,后者是前者的通信基础

compile 'com.squareup.okhttp3:okhttp:3.4.1'
2、具体用法:

Android---OkHttp

Android---OkHttp


3、java代码

private void sendRequestWithOkHttp () {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://www.baidu.com")
                        .build();
                Response response = client.newCall(request).execute();
                String reqponseData = response.body().string();
                showResponse(reqponseData);
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2021-11-20
  • 2021-09-19
  • 2021-12-15
  • 2021-10-14
猜你喜欢
  • 2021-05-03
  • 2022-12-23
  • 2021-09-04
  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案