Android网络框架okhttp3简单封装

1:在demo得build文件中添加依赖(这里以3.8.1版本为例)

dependencies {
    compile 'com.squareup.okhttp3:okhttp:3.8.1'
}

2:对网络框架进行封装(我这里只写了成功和失败两个回调方法)

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 创建日期:
 * 作者:
 * 介绍:网络请求类
 */

public class RequestData {
    myCallback mcallback;//回调接口
    String url = "";//接口路径

    public RequestData(myCallback listener) {
        this.mcallback = listener;
    }

    public void setUrl(String url1) {
        url = url1;
    }
    //定义回调接口
    public interface myCallback {
        void onSuccess(String response);

        void onFail(IOException e);
    }

    //使用GET方法
    public void get() {
        //连接网络要使用线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //GET方法要在路径上写好数据
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                OkHttpClient client = new OkHttpClient.Builder()
                        //连接超时
                        .connectTimeout(1000, TimeUnit.SECONDS)
                        //写入超时
                        .writeTimeout(20, TimeUnit.SECONDS)
                        //读取超时
                        .readTimeout(20, TimeUnit.SECONDS)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //失败做的一些处理
                        if (null != mcallback)
                            mcallback.onFail(e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //获得返回数据
                        String res = response.body().string();
                        if (null != mcallback)
                            mcallback.onSuccess(res);
                    }
                });
            }
        }).start();
    }

    //使用POST方法
    public void post(final String param) {
        //连接网络要使用线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient.Builder()
                        //连接超时
                        .connectTimeout(1000, TimeUnit.SECONDS)
                        //写入超时
                        .writeTimeout(20, TimeUnit.SECONDS)
                        //读取超时
                        .readTimeout(20, TimeUnit.SECONDS)
                        .build();
                //打包表单数据
                FormBody.Builder formBodyBuild = new FormBody.Builder();
                //key值根据需要自己定义
                formBodyBuild.add("param", param);
                //设置请求头
                Request request = new Request.Builder()
                        .url(url)
                        .post(formBodyBuild.build())
                        .build();
                Call call = client.newCall(request);
                request.url();
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //失败做的一些处理,比如连接失败
                        if (null != mcallback)
                            mcallback.onFail(e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //获得返回数据
                        String res = response.body().string();
                        if (null != mcallback)
                            mcallback.onSuccess(res);
                    }
                });
            }
        }).start();
    }
}

3:使用方法

实现接口

implements RequestData.myCallback
直接调用(以get请求为例)
RequestData requestData = new RequestData(this);
requestData.setUrl("接口地址");
requestData.get();
@Override
public void onSuccess(String response) {
     Log.e("111111111111-----------", "请求成功===");
}

@Override
public void onFail(IOException e) {
    Log.e("111111111111-----------", "请求失败===" + e);
}

4:里面也可以添加判断有没有网络、WiFi链接等,如果没得提示无网络链接什么得,搞个dialog,这里就不实现了

你可能感兴趣的:(Android开发功能记录,Android,okhttp3,网络框架)