Java如何调用其他服务器接口
来源:互联网 发布:单片机控制直流电动机 编辑:程序博客网 时间:2024/06/06 17:38
在applicationContext.xml配置:
<!-- 通讯录配置--><bean id="contactService" class="cn.com.qytx.ydzj.impl.ContactImpl"> <!--短信通统计URL--> <constructor-arg value="http://10.200.17.221:8080/txzl/report/SMS_getSMSReport.action"/> <!--语音通知统计URL--> <constructor-arg value="http://10.200.17.221:8080/txzl/report/notice_getNoticeReport.action"/> <!--语音通知列表URL--> <constructor-arg value="http://10.200.17.221:8080/txzl/report/notice_findNoticePageByUserId.action"/> <!--电话会议统计URL--> <constructor-arg value="http://10.200.17.221:8080/txzl/report/meeting_getMeetingReport.action"/> <!--电话会议列表URL--> <constructor-arg value="http://10.200.17.221:8080/txzl/report/meeting_findMeetingPageByUserId.action"/></bean>
在类中引用ContactImpl.java
<pre class="java" name="code">package cn.com.qytx.ydzj.impl;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.persistence.Query;import cn.com.qytx.platform.base.dao.BaseDao;import cn.com.qytx.ydzj.service.IContact;public class ContactImpl implements IContact{private String smsUrl;//短信通统计URL private String noticeUrl;//语音通知统计URL private String noticeListUrl;//语音通知列表URL private String meetingUrl;//电话会议统计URL private String meetingListUrl;//电话会议列表URL public ContactImpl() {super();}public ContactImpl(String smsUrl, String noticeUrl, String noticeListUrl,String meetingUrl, String meetingListUrl) {super();this.smsUrl = smsUrl;this.noticeUrl = noticeUrl;this.noticeListUrl = noticeListUrl;this.meetingUrl = meetingUrl;this.meetingListUrl = meetingListUrl;}@Overridepublic String getSMSReport(String token, String reportTime)throws Exception {BufferedReader br=null;try{URL url=new URL(smsUrl+"?token="+token+"&reportTime="+reportTime);HttpURLConnection httpConnection=(HttpURLConnection)(url.openConnection());httpConnection.connect();InputStream is=httpConnection.getInputStream();br=new BufferedReader(new InputStreamReader(is,"utf-8"));StringBuffer sb=new StringBuffer();while(br.read()!=-1){sb.append(br.readLine());}String content=new String(sb);br.close();br=null;return content;}catch(Exception e){e.printStackTrace();}finally{if(br!=null){br.close();}}return null;}@Overridepublic String getNoticeReport(String token, String reportTime)throws Exception {BufferedReader br=null;try{URL url=new URL(noticeUrl+"?token="+token+"&reportTime="+reportTime);HttpURLConnection httpConnection=(HttpURLConnection)(url.openConnection());httpConnection.connect();InputStream is=httpConnection.getInputStream();br=new BufferedReader(new InputStreamReader(is,"utf-8"));StringBuffer sb=new StringBuffer();while(br.read()!=-1){sb.append(br.readLine());}String content=new String(sb);br.close();br=null;return content;}catch(Exception e){e.printStackTrace();}finally{if(br!=null){br.close();}}return null;}@Overridepublic String findNoticePageByUserId(String token, String userId,Integer pageSize,Integer pageNum)throws Exception {BufferedReader br=null;try{URL url=new URL(noticeListUrl+"?token="+token+"&userId="+userId+"&pageSize="+pageSize+"&pageNum="+pageNum);HttpURLConnection httpConnection=(HttpURLConnection)(url.openConnection());httpConnection.connect();InputStream is=httpConnection.getInputStream();br=new BufferedReader(new InputStreamReader(is,"utf-8"));StringBuffer sb=new StringBuffer();while(br.read()!=-1){sb.append(br.readLine());}String content=new String(sb);br.close();br=null;return content;}catch(Exception e){e.printStackTrace();}finally{if(br!=null){br.close();}}return null;}@Overridepublic String getMeetingReport(String token, String reportTime)throws Exception {BufferedReader br=null;try{URL url=new URL(meetingUrl+"?token="+token+"&reportTime="+reportTime);HttpURLConnection httpConnection=(HttpURLConnection)(url.openConnection());httpConnection.connect();InputStream is=httpConnection.getInputStream();br=new BufferedReader(new InputStreamReader(is,"utf-8"));StringBuffer sb=new StringBuffer();while(br.read()!=-1){sb.append(br.readLine());}String content=new String(sb);br.close();br=null;return content;}catch(Exception e){e.printStackTrace();}finally{if(br!=null){br.close();}}return null;}@Overridepublic String findMeetingPageByUserId(String token, String userId,Integer pageSize,Integer pageNum)throws Exception {BufferedReader br=null;try{URL url=new URL(meetingListUrl+"?token="+token+"&userId="+userId+"&pageSize="+pageSize+"&pageNum="+pageNum);HttpURLConnection httpConnection=(HttpURLConnection)(url.openConnection());httpConnection.connect();InputStream is=httpConnection.getInputStream();br=new BufferedReader(new InputStreamReader(is,"utf-8"));StringBuffer sb=new StringBuffer();while(br.read()!=-1){sb.append(br.readLine());}String content=new String(sb);br.close();br=null;return content;}catch(Exception e){e.printStackTrace();}finally{if(br!=null){br.close();}}return null;}}
然后传送对象json格式
/** * * 功能:电话会议统计报表 */ public void getMeetingReport(){ try{ String startTime=""; String endTime=""; if(StringUtils.isNotBlank(reportTime)){ String[] arrs=reportTime.split("-"); if(arrs.length>1){ Integer year=Integer.parseInt(arrs[0]); Integer month=Integer.parseInt(arrs[1]); Integer day=30; if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ day=31; } if((year%4==0&&year%100!=0)||(year%400==0)){ if(month==2){ day=29; } }else{ if(month==2){ day=28; } } startTime=reportTime+"-01 00:00:00"; endTime=reportTime+"-"+day+" 23:59:59"; } } Map<String,Object> map=reportService.getMeetingReport(token, startTime,endTime); Gson gson=new Gson(); String json=gson.toJson(map); ajax(json); }catch(NoticeException ne){ try { ajax(ne.getMessage()); } catch (Exception e) { e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); } }
完毕!
0 0
- Java如何调用其他服务器接口
- ssm如何调用其他服务器地址接口
- java如何调用其他系统的web service接口
- java如何调用接口
- JAVA如何调用VOS2009接口
- java如何调用http接口
- java如何调用webservice接口
- 如何调用部署在其他服务器上的EJB?
- 如何调用部署在其他服务器上的EJB?
- java 如何调用com组件接口方法
- java如何调用服务端的WSDL接口
- java如何写接口给别人调用
- java如何写接口给别人调用
- java如何写接口给别人调用
- 如何在JAVA程序中调用windows其他程序
- JAVA中如何调用其他类文件中的对象实例
- android调用服务器接口
- 【干货篇】调用其他系统http接口超时了,如何处理,方案汇总
- Spring学习-黎活明视频学习注解
- pom详解
- activity跳转的时候如何实现动画效果
- 空域滤波图示
- How to Cracked Sublime Text 3 Build 3065 in Ubuntu (Linux)
- Java如何调用其他服务器接口
- word break
- Bug敏感度的培养
- ibatis批量查询数据xml配置
- 获取Spring上下文ApplicationContext的多种方法
- 【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
- C#个人总结,第4章:继承
- JAVA字符串String中replace与replaceAll的区别
- 访问函数简单