当前位置:   article > 正文

Android开发OkHttp3的使用_android okttp3 dopost map

android okttp3 dopost map

1、首先添加依赖在这里插入图片描述

implementation ('com.squareup.okhttp3:okhttp:3.10.0')
  • 1

2、测试网址

https://httpbin.org/

3、GET同步请求

public void get_sync(View view) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            Request request = new Request.Builder().url("https://httpbin.org/get?a=1&b=2").build();
            //准备请求的call对象
            Call call = okHttpClient.newCall(request);
            try {
                Response response = call.execute();
                Log.e(TAG, "get_sync: "+response.body().string() );
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

4、GET异步请求

//异步请求
public void get_nosync(View view) {
    Request request = new Request.Builder().url("https://httpbin.org/get?a=1&b=2").build();
    //准备请求的call对象
    Call call = okHttpClient.newCall(request);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            Log.e(TAG, "onFailure: ");
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful())//成功
                Log.d(TAG, "onResponse: " + response.body().string());
            else
            {
                Log.e(TAG, "onFailure: ");
            }
        }

    });

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

5、POST同步请求

public void post_sync(View view) {
   new Thread(new Runnable() {
       @Override
       public void run() {
           RequestBody requestBody = new FormBody.Builder()
                   .add("a", "1")
                   .add("b", "2")
                   .build();

           Request request = new Request.Builder().url("https://httpbin.org/post")
                   .post(requestBody)
                   .build();
           //准备请求的call对象
           Call call = okHttpClient.newCall(request);
           try {
               Response response = call.execute();
               Log.e(TAG, "get_sync: "+response.body().string() );
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }).start();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

6、POST异步请求

public void post_nosync(View view) {
   RequestBody requestBody = new FormBody.Builder()
           .add("a", "1")
           .add("b", "2")
           .build();

   Request request = new Request.Builder().url("https://httpbin.org/post")
           .post(requestBody)
           .build();
   //准备请求的call对象
   Call call = okHttpClient.newCall(request);
   call.enqueue(new Callback() {
       @Override
       public void onFailure(Call call, IOException e) {
           Log.e(TAG, "onFailure: ");
       }

       @Override
       public void onResponse(Call call, Response response) throws IOException {
           if (response.isSuccessful())//成功
               Log.d(TAG, "onResponse: " + response.body().string());
           else
           {
               Log.e(TAG, "onFailure: ");
           }
       }
   });
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/771747
推荐阅读
相关标签
  

闽ICP备14008679号