JAVA原生方法实现简单的异步操作

创建Http.java
public class Http {
    private getReturnClick click;
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message remsg) {
            // TODO Auto-generated method stub
            super.handleMessage(remsg);
            if (remsg.what == 1) {//如果返回1
                click.success((String) remsg.obj);
            } else {
                click.fail();
            }
        }
    };

    public void get(String url1) {
        Message msg = Message.obtain();
        new Thread() {
            public void run() {
                try {

                    URL url = new URL(url1);
                    HttpURLConnection Connection = (HttpURLConnection) url.openConnection();
                    Connection.setRequestMethod("GET");
                    Connection.setConnectTimeout(10000);
                    Connection.setReadTimeout(10000);
                    int responseCode = Connection.getResponseCode();
                    if (responseCode == Connection.HTTP_OK) {
                        InputStream inputStream = Connection.getInputStream();
                        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int length = 0;
                        while ((length = inputStream.read(bytes)) != -1) {
                            arrayOutputStream.write(bytes, 0, length);
                            arrayOutputStream.flush();//强制释放缓冲区
                        }
                        msg.what = 1;
                        msg.obj = arrayOutputStream.toString();
                        mHandler.sendMessage(msg);
                    } else {
                        msg.what = -1;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    msg.what = -1;
                    mHandler.sendMessage(msg);
                }
            }
        }.start();
    }

    public void post(String url1, String data) {
        Message msg = Message.obtain();
        new Thread() {
            public void run() {
                try {
                    URL url = new URL(url1);
                    HttpURLConnection Connection = (HttpURLConnection) url.openConnection();//创建连接
                    Connection.setRequestMethod("POST");
                    Connection.setConnectTimeout(3000);
                    Connection.setReadTimeout(3000);
                    Connection.setDoInput(true);
                    Connection.setDoOutput(true);
                    Connection.setUseCaches(false);
                    Connection.connect();
                    DataOutputStream dos = new DataOutputStream(Connection.getOutputStream());
                    String title = data;//这里是POST请求需要的参数字符串类型,例如"id=1&data=2"
                    dos.write(title.getBytes());
                    dos.flush();
                    dos.close();//写完记得关闭
                    int responseCode = Connection.getResponseCode();
                    if (responseCode == Connection.HTTP_OK) {//判断请求是否成功
                        InputStream inputStream = Connection.getInputStream();
                        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int length = 0;
                        while ((length = inputStream.read(bytes)) != -1) {
                            arrayOutputStream.write(bytes, 0, length);
                            arrayOutputStream.flush();
                        }//读取响应体的内容
                        msg.what = 1;
                        msg.obj = arrayOutputStream.toString();
                        mHandler.sendMessage(msg);
                    } else {
                        msg.what = 0;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    msg.what = 0;
                    mHandler.sendMessage(msg);
                }
            }
        }.start();
    }

    public interface getReturnClick {
        void success(String re);

        //成功事件
        void fail();
        //失败事件
    }

    public void setGetReturnClick(getReturnClick click) {
        this.click = click;
    }

}
使用时创建对象直接使用
//GET/POST取源
Http http = Http();
http.get("网址");
http.post("网址","传递参数");
http.setGetReturnClick(new Http.getReturnClick(){
	@Override
	public void success(String text){
		//获取成功后执行的事件
	}
	
	@Override
	public void fail(){
		//获取失败后执行的事件
	}
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A先生不会提交flag

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值