您的位置:首页 > 编程语言 > Java开发

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();  } }
完毕!

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java SSH