Okhttp网络请求

       布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dash.a02_okhttp.MainActivity">

    <Button
        android:text="get同步"
        android:onClick="getTongBu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="get异步"
        android:onClick="getYiBu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="post异步"
        android:onClick="postYiBu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:text="post上传"
        android:onClick="postShangChuan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

                 get同步异步请求

  public void getTongBu(View view) {

        new Thread(){
            @Override
            public void run() {
                //1.创建一个okhttp客户端对象
                OkHttpClient okHttpClient = new OkHttpClient();

                //2.创建一个请求的对象....默认就是get方式
                Request request = new Request.Builder()
                        .url("https://www.zhaoapi.cn/ad/getAd")
                        .build();
                //3.客户端要去调用一个请求的对象
                Call call = okHttpClient.newCall(request);

                //4.执行....指定同步还是异步请求的方式....call.execute()同步的方式
                try {
                    Response response = call.execute();

                    if (response.isSuccessful()){

                        /**
                         * * 响应体的 string() 方法对于小文档来说十分方便、高效。但是如果响应体太大(超过1MB),
                         * 应避免适应 string()方法 ,因为他会将把整个文档加载到内存中。
                         * 对于超过1MB的响应body,应使用流的方式来处理body。
                         */
                        Log.i("------",response.body().string());

                        //设置适配器....handler...runOnUiThread()

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



    }

    public void getYiBu(View view) {

        OkHttpClient okHttpClient = new OkHttpClient();

        final Request request = new Request.Builder()
                .url("https://www.zhaoapi.cn/ad/getAd")
                .build();

        Call call = okHttpClient.newCall(request);

        //指定call调用的方式
        call.enqueue(new Callback() {
            //失败
            @Override
            public void onFailure(Call call, IOException e) {
                //打印异常的日志
                e.printStackTrace();
            }

            //服务器有响应....这个位置仍然是工作线程
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){

                    //response.body().string();

                    //java.lang.IllegalStateException: closed 非法状态异常:关闭...本次请求已经响应,,,关闭
                    Log.i("------",response.body().string());

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,"请求成功",Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        });

    }

                       post异步请求

 /**
     * post和get都有同步和异步的方式.....区别就在于call调用的方法不同
     * @param view
     */
    public void postYiBu(View view) {

        OkHttpClient okHttpClient = new OkHttpClient();

        //2.传递参数使用FormBody请求实体对象
        FormBody formBody = new FormBody.Builder()
                .add("mobile", "15715317583")
                .add("password", "123456")
                .build();

        //3.获取post方式的请求对象
        Request request = new Request.Builder()
                .post(formBody)
                .url("https://www.zhaoapi.cn/user/reg")
                .build();
        //4.
        Call call = okHttpClient.newCall(request);

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

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                if (response.isSuccessful()){

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Toast.makeText(MainActivity.this,response.body().string(),Toast.LENGTH_SHORT).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        });

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值