java8Lambda模拟Jq.Ajax简单实现

package com.slient.lambda;

import java.io.IOException;
import java.util.Arrays;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class CallBackTest {
    public static void main(String[] args) throws ClientProtocolException, IOException {
        _JqueryAjax.ajax(
            "https://www.baidus.com",
            "POST",
            true,
            (xhr) -> {
                //对外暴露的对于请求的设置
            },
            (res) -> {
                System.out.println("Success CallBack >>> " + res);
            },
            (err) -> {
                System.out.println("Error CallBack >>> " + err.getMessage());
            }
        );
        System.out.println("================================测试异步分割线================================");
    }
}
/**
 * Jdk1.8,模拟JQ.ajax的简单实现
 * @author zhengfu
 *
 */
class _JqueryAjax {
    //beforeSend
    @FunctionalInterface
    interface _IBeforeSend {
        void beforeSend(HttpRequestBase httpRequestBase);
    }
    //success
    @FunctionalInterface
    interface _ISuccess {
        void success(Object obj);
    }
    //error
    @FunctionalInterface
    interface _IError {
        void error(Throwable err);
    }
    //
    public static void ajax(
            String url,
            String type,
            boolean async,
            _IBeforeSend b,
            _ISuccess s,
            _IError e) {
        //模拟异步
        if(async) {
            new Thread(() -> {
                send(url,type,b,s,e);
            }).start();
        }else {
            send(url,type,b,s,e);
        }
    }
    //
    private static void send(
            String url,
            String type,
            _IBeforeSend b,
            _ISuccess s,
            _IError e) {
        //
        CloseableHttpClient client = HttpClientBuilder.create().build();
        //
        HttpRequestBase httpRequestBase = null;
        switch (type) {
            case "POST":
                    httpRequestBase = new HttpPost(url);
                break;
            case "GET":
                    httpRequestBase = new HttpGet(url);
                break;
        }
        //
        b.beforeSend(httpRequestBase);
        //execute
        CloseableHttpResponse response = null;
        try {
            //
            response = client.execute(httpRequestBase);
            //
            s.success(EntityUtils.toString(response.getEntity()));
        } catch (Exception exception) {
            //
            e.error(exception);
        } finally {
            close(response);
            close(client);
        }
    }
    //关闭资源
    private static <T extends java.io.Closeable> void close(T t) {
        try {
            if(t != null)
                t.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值