美文网首页
基于OkHttp封装的网络请求工具类

基于OkHttp封装的网络请求工具类

作者: 小嘜 | 来源:发表于2019-03-05 21:36 被阅读0次

1、代码

public class OceanUtil {

    private String url;
    private RequestBody requestBody;
    private HttpRequestCallback httpRequestCallback;
    private static OkHttpClient okHttpClient;

    // 构造方法根据需求自定义封装
    public OceanUtil(String type, JSONObject jsonObject, HttpRequestCallback httpRequestCallback) {
        String domain = "http://192.168.43.108:8088/transportservice/action/";
        this.url = domain + type;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonObject.toString());
        this.requestBody = requestBody;
        this.httpRequestCallback = httpRequestCallback;
        okHttpClient = getOkHttpClient();
    }

    // 同步请求
    public void AsynPostInfo() {
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            if(response.isSuccessful())
            {
                String str = response.body().string();
                httpRequestCallback.callback(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void postInfo() {
        Request request = new Request.Builder().url(url).post(requestBody).build();
        okHttpClient.dispatcher().setMaxRequests(1);
        okHttpClient.dispatcher().setMaxRequestsPerHost(1);
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                httpRequestCallback.callback(str);
            }
        });
    }

    public void postInfo(final int time) {
        Request request = new Request.Builder().url(url).post(requestBody).build();
        okHttpClient.dispatcher().setMaxRequests(1);
        okHttpClient.dispatcher().setMaxRequestsPerHost(1);
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                httpRequestCallback.callback(str);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                postInfo(time);
            }
        });
    }

    private synchronized OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        return okHttpClient;
    }


    public interface HttpRequestCallback {
        void callback(String strResponse);
    }

}

相关文章

网友评论

      本文标题:基于OkHttp封装的网络请求工具类

      本文链接:https://www.haomeiwen.com/subject/ngrauqtx.html