一个简单的回调(例子)

1.声明一个回调Interface:


public interface CallBack {
/**
* 执行回调方法
* @param objects 将处理后的结果作为参数返回给回调方法
*/
public void execute(Object... objects );
}

2.回调的地方继承回调,实现回调的方法:


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.spi.http.HttpContext;

public class testServlet extends HttpServlet implements CallBack{

NetTool netTool;
HttpServletResponse globalResponse;
String netResult;
/**
* Constructor of the object.
*/
public testServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

globalResponse = response;
netTool = new NetTool();

netTool.sendGet("", this);
}

public void outPut(HttpServletResponse response){
response.setContentType("text/html");
response.setHeader("content-type", "text/html;charset=UTF-8");
PrintWriter out;
try {
out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" ");
out.print(this.getClass()+" : \n");
out.print(netResult);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

@Override
public void execute(Object... objects) {
// TODO Auto-generated method stub
try {
String result = (String)objects[0];
netResult = new String(result.getBytes(), "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outPut(globalResponse);
}

}

3.回调自己:


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class NetTool {

public NetTool(){
super();
}

public void sendGet(String url, CallBack callBack) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = "http://m.weather.com.cn/data/101010100.html";
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
// connection.setRequestProperty("accept", "*/*");
// connection.setRequestProperty("connection", "Keep-Alive");
// connection.setRequestProperty("user-agent",
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
/*
* Map<String, List<String>> map = connection.getHeaderFields(); //
* 遍历所有的响应头字段 for (String key : map.keySet()) {
* System.out.println(key + "--->" + map.get(key)); }
*/
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
callBack.execute(result);
}
}

一个简单的回调(例子),布布扣,bubuko.com

时间: 2024-08-02 06:59:58

一个简单的回调(例子)的相关文章

使用Multiplayer Networking做一个简单的多人游戏例子-2/3(Unity3D开发之二十六)

猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51007512 使用Multiplayer Networking做一个简单的多人游戏例子-1/3 使用Multiplayer Networking做一个简单的多人游戏例子-2/3 使用Multiplayer Networking做一个简单的多人游戏例子-3/3 7. 在网络中控制Player移动 上一篇中,玩家操

一个简单的KVO例子

一个简单的KVO例子. 两个界面,第一个界面显示名字和配偶(spouse)名字,第二个界面显示修改名字和配偶名字,返回时,将看到第一个界面的名字显示发生改变. 首先定义一个person类作为model. #import <Foundation/Foundation.h> @interface Person : NSObject @property (strong, nonatomic) NSString *name; @property (strong, nonatomic) NSString

Java一个简单的死锁例子

内容:一个简单的死锁例子,大概的思路:两个线程A和B,两把锁X和Y,现在A先拿到锁X,然后sleep()一段时间,我们知道sleep()是不会释放锁资源的.然后如果这段时间线程B拿到锁Y,也sleep()一段时间的话,那么等到两个线程都醒过来的话,那么将互相等待对方释放锁资源而僵持下去,陷入死锁.flag的作用就是让A和B获得不同的锁. public class TestDeadLock { public void run() { MyThread mt = new MyThread(); ne

编写一个简单的jdbc例子程序

1 package it.cast.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 9 public class Base { 10 11 public static void main(String[] args) th

一个简单的小例子让你明白c#中的委托-终于懂了!

模拟主持人发布一个问题,由多个嘉宾来回答这个问题. 分析:从需求中抽出Host (主持人) 类和Guests (嘉宾) 类. 作为问题的发布者,Host不知道问题如何解答.因此它只能发布这个事件,将事件委托给多个嘉宾去处理.因此在Host 类定义事件,在Guests类中定义事件的响应方法.通过多番委托的"+="将响应方法添加到事件列表中,最终 Host 类将触发这个事件.实现过程如下: 代码其实很少下面贴出来所有代码: QuestionArgs.cs view plaincopy to

一个简单的cmake例子

一个简单的cmake例子CMakeLists.txt,生成动态库文件,可以指定发布目录. 尚不支持: 1.交叉编译环境配置 2.添加依赖库 1 #在当前目录新建一个build目录,然后cd build:cmake .. 2 #这样的好处是,可以将cmake生成的内容,和源码文件分离 3 4 #定义好版本需求 5 cmake_minimum_required (VERSION 2.6) 6 #工程名字 7 project (Libtree) 8 #编译结果发布路径 9 set ( CMAKE_IN

Servlet学习教程(三)---- 一个简单的Servlet例子

我们用个最简单的Servlet例子来解说一下Servlet简单配置以及Servlet类实现类的写法. 第一,我们新建一个Dynamic Web Project,起名Servlet 点击NEXT,设置Default output folder 为Servlet/WebContent/WEB-INF/classes 第二,创建一个包,包名为Servlet,然后创建一个类名为WelcomeServlet类.(Servlet类当然缺少不了Servlet容器,请注意你的开发软件是否已经集成Servlet容

一个简单的springmvc例子 入门

一直是从事棋牌游戏,平常用的东西 大多数只是使用一些javase的一些 api对spring 这方面 用到的比较少,每次学了都忘,始终记不住.为了 更轻松学习springboot,从新学习了spring,学到springmvc记录一下学习过程,先从一个简单的例子入门. 首先说下springmvc:和Struts2 都是一个mvc的框架 属于spring 的后续产品 springmvc和Struts2的对比: 原文地址:https://www.cnblogs.com/920913cheng/p/1

一个简单的SignalR例子

本文介绍如何使用SignalR的Hub制作一个简单的点赞页面.不同浏览器(或者不同窗口)打开同一个页面,在任何一个页面点赞,所有页面同时更新点赞数. 1.使用Visual Studio Community 2013新建一个Web项目,选择空白模板. 2.使用NugGet安装SignalR. 3.添加Startup.cs. 代码如下. namespace SignalRDemo { public class Startup { public void Configuration(IAppBuild