OKHttp异步get,post请求和同步请求

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


//        postEnqueue();


    }


    //
    private void enqueue(){


//      创建发送求的client 对象
        OkHttpClient client = new OkHttpClient();


        // 创建一个request 对象 ,request 对象 构造包含 请求方法 (get post put delete),请求接口地址
        Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts?pscid=39&page=1").build();


//        newCall 返回Call(实际上 RealCall ) 对象,实际底层 把网络请求放入了一个请求队列
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//失败回调
            }


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


//                成功回调


//                System.out.println("call = " + Thread.currentThread().getName());
                System.out.println("response = " + response.body().string());


            }
        });
    }




    private void postEnqueue(){


        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10,TimeUnit.SECONDS)
                .connectTimeout(10,TimeUnit.SECONDS)
                .build();


//        FormBody 表单
        RequestBody requestBody = new FormBody.Builder()
                .add("pscid","39")
                .add("page","1")
                .build();


        Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts")
                .post(requestBody)
                .build();


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


            }


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




                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, "1111", Toast.LENGTH_SHORT).show();
                    }
                });
                System.out.println("response = " + response.body().string());


            }
        });
    }




    private void execute(){


        //同步方法


        new Thread(new Runnable() {
            @Override
            public void run() {


                try {
                    OkHttpClient client = new OkHttpClient();


                    Request request = new Request.Builder().url("http://120.27.23.105/product/getProducts?pscid=39&page=1").build();


                    Call call =  client.newCall(request);


                    Response response =  call.execute() ;


                    String result =  response.body().string();


                    System.out.println("result = " + result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值