安卓入门OkHttp使用

OkHttp使用

官网

https://github.com/square/okhttp

文档已经404了

GET请求

使用步骤

  • 创建 请求的Builder
  • 设置 Builder 的请求地址
  • 创建 Request 构造
  • 执行请求newCall

第一种同步请求

new Thread() {
        @Override
        public void run() {
                //构建
                Request.Builder builder=new Request.Builder();
                builder.url("https://www.baidu.com");
                //请求
                Request request=builder.build();
                //客户端
                Call call = okHttpClient.newCall(request);
                try {
                        Response response = call.execute();
                        if(response.isSuccessful())
                        {
                                String res = response.body().string();
                                Message msg = new Message();
                                msg.what = 0x123;
                                msg.obj = res;
                                handler.sendMessage(msg);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                }

                super.run();
        }
}.start();

异步请求

Request.Builder builder=new Request.Builder();

builder.url("https://www.baidu.com");
Request request=builder.build();
Call call=okHttpClient.newCall(request);
final Message msg=new Message();
call.enqueue(new Callback() {
        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {

        }

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                msg.what=0x123;
                msg.obj=response.body().string();
                handler.sendMessage(msg);
        }
});

Handler 接受结果返回

final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x123) {
               // msg(msg.obj.toString());
                TextView content=findViewById(R.id.content);
                Log.i("-------------",msg.obj.toString());
                content.setText("hello"+msg.obj.toString());
            }
        }
    };

POST请求

使用步骤

  • 创建 请求的Builder
  • 设置 Builder 的请求地址
  • 设置 BuilderPOST请求,并且携带POST参数
  • 创建 Request 构造
  • 执行请求newCall
 Request.Builder builder=new Request.Builder();
        builder.url("a.php");
        //设置请求参数
        FormBody body=new FormBody.Builder().add("nickname","小米").add("age","30").build();
        //设置POST
        builder.post(body);
        Request request=builder.build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

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

评论区 (0)

没有记录
支持 markdown,图片截图粘贴拖拽都可以自动上传。
空气

空气 · 中级学士

热卖技术,喜欢新东西。

魔童降世
查看更多

最新视频课程

钻级赞助商