客户端:
public class HttpUtil { //创建HttpClient对象 public static HttpClient httpClient = new DefaultHttpClient(); public static final String BASE_URL="http://xxx.xxxx.xx.xx:8080/ticket/"; // public static final String BASE_URL="http://xxx.xxxx.xx.xx:8080/apk/"; // public static final String BASE_URL = "www.baidu.com"; /** * * @param url 发送请求的Url * @return 服务器响应的字符串 * @throws Exception * @throws InterruptedException */ public static String getRequest(final String url) throws Exception { FutureTask<String> task = new FutureTask<String>( new Callable<String>() { @Override public String call() throws Exception { //创建HttpGet对象 HttpGet get = new HttpGet(url); //发送GET请求 HttpResponse response = httpClient.execute(get); //若是服务器响应成功 if(response.getStatusLine(). getStatusCode() == 200) { //获取服务器响应的字符串 String result = EntityUtils. toString(response.getEntity()); return result; } return null; } } ); new Thread(task).start(); return task.get(); } /** * * @param url 发送请求的url * @param rawParams 请求参数 * @return 响应的字符串 * @throws Exception */ public static String postRequest(final String url, final Map<String,String> rawParams) throws Exception { // ExecutorService exec=Executors.newCachedThreadPool(); // FutureTask<String> task = new FutureTask<String>( // new Callable<String>() { // @Override // public String call() throws Exception { //创建HttpPost对象 HttpPost post = new HttpPost(new URI(url)); //对较多的传递参数进行封装、 List<NameValuePair> params = new ArrayList<NameValuePair>(); for(String key : rawParams.keySet()) { //封装请求参数 params.add(new BasicNameValuePair(key, rawParams.get(key))); } //设置请求参数 post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); //发送post请求 HttpResponse response = httpClient.execute(post); //若是服务器响应成功 if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ //获取服务器响应的字符串 String result = EntityUtils.toString(response.getEntity(),HTTP.UTF_8); return result; }else{ return "-1"; } } // } // ); // new Thread(task).start(); // return task.get(); // } }
注意:连接本地服务器时,最好使用ipv4地址而不是localhost。
服务端:
写一个servlet接收,判断客户端发送的是什么请求uri
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String uri = request.getRequestURI(); uri = uri.substring(uri.lastIndexOf("/")); System.out.println("uri: " + uri); if("/hotStation.do".equals(uri)) { doHotStationList(request, response); } if("/stationList.do".equals(uri)) { doStationList(request, response); } }
如doHotStationList是这样写的:
public void doHotStationList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); List<String> hotStationList = StationService.getHostStationList(); JSONArray jsonArray = JSONArray.fromObject(hotStationList); System.out.println("传给客户端:" + jsonArray.toString()); response.getWriter().println(jsonArray.toString()); }
我这里数据交互使用的是json.
客户端获取服务端传递过来的json数据再解析成pojo对象即可。
相关推荐
本项目是某国企的安卓OA客户端...与服务端交互使用的json。服务端是用的j2ee没有使用Web Service。另外项目中还封装了一些文件操作类,下载升级功能也都是有的 涉及模块&技术 json 文件操作 日历 桌面小工具 登录 ...
Activity Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互为了完成某项任务,是一个负责与用户交互的组件 SSH 为 Struts+Spring+Hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架。...
本项目是某国企的安卓OA客户端...与服务端交互使用的json。服务端是用的j2ee没有使用Web Service。另外项目中还封装了一些文件操作类,下载升级功能也都是有的 涉及模块&技术 json 文件操作 日历 桌面小工具 登录 ...
图片本身的工具类,来自疯狂Java联盟。 根据游戏状态来绘制游戏界面上的全部方块。 24、Fireworks-烟花特效 共3个目标文件!~自定义了一个花类,使用SurfaceView绘画。 25、YQ即时聊天 有服务端以及客户端,...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
Java二进制IO类与文件复制操作实例,好像是一本书的例子,源代码有的是独立运行的,与同目录下的其它代码文件互不联系,这些代码面向初级、中级Java程序员。 Java访问权限控制源代码 1个目标文件 摘要:Java源码,...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...
PortGroper 是一款java写的开源拒绝服务测试工具,它不是僵尸网络类的ddos,而是使用大量的代理作为bots发起DDOS。Port Groper可以与用测试防火墙,干扰web 统计脚本的跟踪,为网站增加流量..往好了用什么都能干,就是...