Java如何调用其他服务器接口

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

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值