19软件34 Okhttp3的使用

注册码云帐号 新建仓库

19软件34 Okhttp3的使用_第1张图片
19软件34 Okhttp3的使用_第2张图片
19软件34 Okhttp3的使用_第3张图片
在这里插入图片描述

电脑上要安装svn客户端

推荐tortoisesvn
19软件34 Okhttp3的使用_第4张图片
19软件34 Okhttp3的使用_第5张图片
19软件34 Okhttp3的使用_第6张图片
要把安卓项目建在文件夹的内部

19软件34 Okhttp3的使用_第7张图片
上传demo项目
注意 build app/build app/libs .idea .gradle不要提交

添加OkHttp依赖

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

19软件34 Okhttp3的使用_第8张图片

// 使用okhttp的异步get请求
    private void useOkhttpAsynGet() {
     
        String url = "http://www.qq.com";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url).get().build();
        Call call =  okHttpClient.newCall(request);

        // 异步 需要将call加入请求队列
        call.enqueue(new Callback() {
     
            @Override
            public void onFailure(Call call, IOException e) {
     
                Log.e("useOkhttpAsynGet", e.toString());
            }

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

    }
// 使用okhttp的同步get请求
    private void useOkhttpSyncGet() {
     
        String url = "http://www.qq.com";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url).get().build();
        Call call =  okHttpClient.newCall(request);

        // 同步 需要新开一个线程 进行call调用
        new Thread(new Runnable() {
     
            @Override
            public void run() {
     
                try {
     
                    Response response = call.execute();
                    Log.e("useOkhttpSyncGet", response.body().string());
                } catch (IOException e) {
     
                    e.printStackTrace();
                }
            }
        }).start();

    }

处理显示问题

@Override
            public void onResponse(Call call, Response response) throws IOException {
     
                String content = response.body().string();
                Log.e("useOkhttpAsynGet", content);
                // 还是子线程 不能直接显示 会崩溃
                // tv_content.setText(content);

                // 使用消息机制解决
                Message message = new Message();
                message.what = MSG_GET_CONTENT;
                message.obj = content;
                myHandler.sendMessage(message);
            }
// 初始化Handler
    private void initHandler() {
     
        myHandler = new Handler(new Handler.Callback() {
     
            @Override
            public boolean handleMessage(@NonNull Message msg) {
     
                if(msg.what ==  MSG_GET_CONTENT){
     
                    String content = (String) msg.obj;
                    tv_content.setText(content);
                    return true;
                }

                if(msg.what == MSG_GET_IMG){
     
                    Bitmap bitmap = (Bitmap) msg.obj;
                    iv_show.setImageBitmap(bitmap);
                    return true;
                }

                return false;
            }
        });
    }

使用Post查询归属地

// 使用Post查询归属地
    private void useOkhttpAsynPost() {
     
        String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
                //?mobileCode=13678901234&userID=";
        RequestBody requestBody = new FormBody.Builder()
                .add("mobileCode", "15899998888")
                .add("userID","")
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
     
            @Override
            public void onFailure(Call call, IOException e) {
     
                Log.e("useOkhttpAsynPost", e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
     
                String content = response.body().string();
                Log.e("useOkhttpAsynPost", content);
                // 还是子线程 不能直接显示 会崩溃
                // tv_content.setText(content);

                // 使用消息机制解决
                Message message = new Message();
                message.what = MSG_GET_CONTENT;
                message.obj = content;
                myHandler.sendMessage(message);
            }
        });
    }

使用Okhttp获取并显示图片

// 使用Okhttp获取并显示图片
    private void useOkhttpGetImage() {
     
        String url = "http://www.gdcp.edu.cn/service?token=d0b7d1ee6be74f0dafe02a03f434d184";
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url).get().build();
        Call call =  okHttpClient.newCall(request);

        // 异步 需要将call加入请求队列
        call.enqueue(new Callback() {
     
            @Override
            public void onFailure(Call call, IOException e) {
     
                Log.e("useOkhttpAsynGet", e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
     
                InputStream inputStream = response.body().byteStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                // 使用消息机制解决
                Message message = new Message();
                message.what = MSG_GET_IMG;
                message.obj = bitmap;
                myHandler.sendMessage(message);
            }
        });

    }

你可能感兴趣的:(19软件34 Okhttp3的使用)