其他分享
首页 > 其他分享> > OkHttp常见场景及用法

OkHttp常见场景及用法

作者:互联网

前言:

OkHttp在Android上为我们提供了第三方框架里网络请求的最佳使用方式。下面简略看其使用方法。

依赖:implementation 'com.squareup.okhttp3:okhttp:3.10.0'

注意事项:

Get请求:

说明:
首先我们先构建出一个OkHttpClient()对象实例;然后通过链式调用创建一个Request对象,URL为http://www.fynu.edu.cn/,后接两个参数,其键与值分别为"account"、“password”,“admin”、“467”;之后调用OkHttpClient()对象实例的newCall()方法创建Call()方法,并调用其execute()来返回其从服务器获得的数据。

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
                  .url("https://www.fynu.edu.cn/?account=amdmin&password=467")
                  .build();
Response response = client.newCall(request).execute();
String data = response.body().string();

Post请求:

说明:
Post请求与Get请求多了表单这个概念,其实就是特殊的键值对。需要说明的是,这个表单内的数据近似无限多(字符占用很少,但有的浏览器限制为几兆比以内)。下面的add()方法内第一个就是键,第二个就是值,其余均类似,不再赘述。

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
                          .add("id","2012001")
                          .add("paswword","123456789")
                          .build();

Request request = new Request.Builder()
                  .url("https://www.test.com/NoneTest_war_exploded/test")
                  .post(requestBody)
                  .build();
Response response = client.newCall(request).execute();
String data = response.body().string();

回调方法:

Post与Get均有两个回调方法,一个是请求成功的方法onResponse(),另一个是请求失败的方法onFailure()。下面以Post请求为例:

OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
                            .add("id","2012001")
                            .build();

Request request = new Request.Builder()
                            .url("http://192.168.137.1:8080/NoneTest_war_exploded/test")
                            .post(requestBody)
                            .build();

Call call = client.newCall(request);
call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String data = response.body().string();
    }
});

完整示例:

    private void requestTest(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("id","2012001")
                            .build();

                    Request request = new Request.Builder()
                            .url("http://192.168.137.1:8080/NoneTest_war_exploded/test")
                            .post(requestBody)
                            .build();

                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            final String data = response.body().string();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                // According to data of reponse to update UI thread.
                                    textView.setText(data);
                                }
                            });
                        }
                    });

                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

标签:场景,request,Request,OkHttpClient,call,OkHttp,new,用法,response
来源: https://blog.csdn.net/kicinio/article/details/110007498