Java如何调用其他服务器接口
2014-09-28 14:05
483 查看
在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; } @Override public 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; } @Override public 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; } @Override public 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; } @Override public 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; } @Override public 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(); } }
完毕!
相关文章推荐
- ssm如何调用其他服务器地址接口
- java 如何调用com组件接口方法
- 如何调用部署在其他服务器上的EJB?
- 如何在JAVA程序中调用windows其他程序
- java如何调用服务端的WSDL接口
- Java中,如何调用“其他类的方法中生成的实例化的对象”的属性?
- Cocos2dx引擎笔记——如何让Java和C++接口互相调用:JNI使用指南
- java如何调用https地址类型的webservice接口
- express:webpack dev-server开发中如何调用后端服务器的接口?
- 【干货篇】调用其他系统http接口超时了,如何处理,方案汇总
- java本地方法如何调用其他程序函数,方法详解
- java如何调用webservice接口
- java如何调用服务端的WSDL接口
- JAVA如何调用VOS2009接口
- java本地方法如何调用其他程序函数,方法详解2
- JAVA如何调用VOS2009接口
- 如何调用部署在其他服务器上的EJB?
- java如何调用其他系统的web service接口
- java如何写接口给别人调用
- 如何在Tomcat或其他基于Java的Web服务器下安装SSL证书