android中OkHttp的GET请求

现在的android开发绝大多数都使用okhttp请求数据。

okhttp必须在安装完所有okhttp所需环境文件后才能过使用,分为okhttp.jar和okio.jar,下载地址为:"http://square.github.io/okhttp/"。


okhttp的get请求分为4步:

1.建立okhttp对象

OkHttpClient okhttpclient = new OkHttpClient();
2.构造request

Request.Builder buider = new Request.Builder();
Request request = buider.get().url("https://www.imooc.com/").build();

3.将request封装成Call

Call call = okhttpclient.newCall(request);

4.执行Call(异步 ps:此操作不能操作UI)

call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
      
    }
});



猜你喜欢

转载自blog.csdn.net/ct_ts/article/details/79218959