OkHttp

来源:互联网 发布:制作linux启动u盘 编辑:程序博客网 时间:2024/05/02 01:49

public class HttpUtil {

 public static byte[] download(String path) throws IOException {

  // 创建一个OkHttpClient对象
  OkHttpClient client = new OkHttpClient();
  // 时间;时间的单位
  client.setConnectTimeout(5, TimeUnit.SECONDS);
  // 默认就是get请求.
  Request request = new Request.Builder().url(path).build();
  // 类似于HttpClient中的request()方法.执行一个请求
  Response response = client.newCall(request).execute();
  // 网络访问成功
  if (response.isSuccessful()) {
   // 得到一个响应体
   ResponseBody body = response.body();
   // 得到一个字节数组结果
   return body.bytes();
   // InputStream byteStream = body.byteStream();
   // String string = body.string();
  }

  return null;
 }
}

0 0