java开发调用其它项目接口

    public Object getAlarmsIp(String Indevice_ip) {
        /*
         *第一种方式post请求
         */
        JSONObject result = new JSONObject();
        JSONObject jsons = new JSONObject();
        jsons.put("key", "value");
        JSONObject jsonss = null;
        try {
            URL url = new URL("你要调用其它项目的接口路径");
            //打开和url之间的连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            PrintWriter out = null;
            //请求方式 这里设置你需要请求的方式
            //conn.setRequestMethod("POST");
            //设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            //这里设置请求头
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            //设置是否向httpUrlConnection输出,设置是否从httpUrlConnection读入,此外发送post请求必须设置这两个
            //最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,
            //post与get的 不同之处在于post的参数不是放在URL字串里面,而是放在http请求的正文内。
            conn.setDoOutput(true);
            conn.setDoInput(true);
            //获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());

            //设置你接口需要传入的参数
            Map map = new HashMap();
            map.put("device_ip", Indevice_ip);
            String toJSONString = JSON.toJSONString(map);

            //发送请求参数即数据
            out.print(toJSONString);
            //缓冲数据
            out.flush();
            //获取URLConnection对象对应的输入流
            InputStream is = conn.getInputStream();
            //构造一个字符流缓存
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String str = "";

            while ((str = br.readLine()) != null) {
                jsonss = com.alibaba.fastjson.JSONObject.parseObject(str);
            }
            RpiIpranAlarmInfo rpiIpranAlarmInfo = new RpiIpranAlarmInfo();
            String getdevice_ip=Indevice_ip;
            String serviceFlag=jsonss.getString("serviceFlag");
            String device_manu=jsonss.getString("device_manu");
            String serviceMessage=jsonss.getString("serviceMessage");
            String alarm_name="";
            String device_name="";
            String device_ip="";
            String alarm_time="";
            String update_time="";
            JSONArray data = jsonss.getJSONArray("data");
		
		//循环获取json中data的数据
            if(data.size()>0){
                for (int i = 0; i < data.size(); i++) {
                    JSONObject jsonObject = data.getJSONObject(i);
                    alarm_name=jsonObject.getString("alarm_name");
                    device_name=jsonObject.getString("device_name");
                    device_ip=jsonObject.getString("device_ip");
                    alarm_time=jsonObject.getString("alarm_time");
                    update_time=jsonObject.getString("update_time");
                }
                rpiIpranAlarmInfo.setAlarmName(alarm_name);
                rpiIpranAlarmInfo.setDeviceName(device_name);
                rpiIpranAlarmInfo.setDeviceIp(device_ip);
                rpiIpranAlarmInfo.setAlarmTime(alarm_time);
                rpiIpranAlarmInfo.setUpdateTime(update_time);
            }

            rpiIpranAlarmInfo.setIfaceName("alarms_ip");
            rpiIpranAlarmInfo.setInDeviceIp(getdevice_ip);
            rpiIpranAlarmInfo.setServiceflag(serviceFlag);
            rpiIpranAlarmInfo.setServicemessage(serviceMessage);
            rpiIpranAlarmInfo.setDeviceManu(device_manu);

            iRpiIpranAlarmInfoService.save(rpiIpranAlarmInfo);

            System.out.println("获取的数据:" + jsonss);
            //关闭流
            is.close();
            //断开连接,最好写上,disconnect是在底层tcp socket链接空闲时才切断。如果正在被 其他线程使用就不切断。
            //固定多线程的话,如果不disconnect,链接会增多,直到收发不出信息。写上 disconnect后正常一些。
            conn.disconnect();
            System.out.println("结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonss;
    }

 @Scheduled(cron = "0 0/10 * * * ?")
    public Object addOne() {
        QueryWrapper<RpiIpranIfaceInfo> qw = new QueryWrapper<>();
        qw.eq("IFACE_NAME", "alarms_ip");
        List<RpiIpranIfaceInfo> rpiIpranIfaceInfo = null;
        Object getAlarmsIp=null;
        rpiIpranIfaceInfo = rpiIpranIfaceInfoMapper.selectList(qw);
        if(rpiIpranIfaceInfo.size()>0){
            for (int i = 0; i < rpiIpranIfaceInfo.size(); i++) {
                RpiIpranIfaceInfo rpiIpranIfaceInfo1 = rpiIpranIfaceInfo.get(i);
                String line_no1 = rpiIpranIfaceInfo1.getInInfo();
                String Indevice_ip=line_no1.trim().split(":")[1].substring(0);
                //将你要传入的参数传入
                getAlarmsIp = getAlarmsIp(Indevice_ip);

            }
        }
        return getAlarmsIp;
    }
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ps:主要解决在webservice中,我们想从另外一个项目调用webservice项目接口,也就是跨项目调用接口 这里主要用到了xfire wsdl 废话不说了 直接上东西 1. 首先新建一个项目 2. 在src下创建两个文件: a) 第一个是你想要访问的webservice的接口,比如我想访问的接口是 ReleaseService 那就在当前项目创建一个ReleaseService接口(接口中的方法必须和你想要访问的webservice的接口中的方法相同) b) 第二个是你的调用类 3. 导入相应的jar包,这些包不能引用,一定要复制到lib文件夹下面在引用 4. 具体的实现代码 TestWebService方法的代码: package com.isanta.webServiceTest; import java.io.InputStream; import java.net.MalformedURLException; import java.util.Properties; import java.util.Scanner; import org.codehaus.xfire.XFireFactory; import org.codehaus.xfire.client.XFireProxyFactory; import org.codehaus.xfire.service.Service; import org.codehaus.xfire.service.binding.ObjectServiceFactory; public class TestWebService { /** * @param args */ public static void testWebService() throws MalformedURLException, Exception{ // TODO Auto-generated method stub /** *这里是我的参数放在了properties文件中,我在读取里面的参数,这里我们也可以通过方法传参数 *如 : testWebService(String url,String xMlStr)() 那么在调用的时候就可以直接传进来了 *url 是你访问的webservice 的tomcat 的服务器地址 */ Properties pro = new Properties(); InputStream in = null; in = TestWebService.class.getResourceAsStream("/request.properties"); pro.load(in); String url = pro.getProperty("url"); String xMLstr = pro.getProperty("xMLstr"); Service s=new ObjectServiceFactory().create(ReleaseService.class); XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire()); System.out.println("url="+url); try { //这里就是获取webservice的接口的实例对象 ReleaseService seleaseService=(ReleaseService) xf.create(s,url); System.out.println("进入接口----------------->请求报文:"+xMLstr); //这里就是调用你需要的接口的方法 String st=seleaseService.queryReceiptDatas(xMLstr); System.out.print(st); } catch(Exception e) { e.printStackTrace(); } } } 5. 将整个项目打包成jar 6. 将打好的jar包引入到你想要调用项目中,然后就想 正常的代码一样来调用,如: import java.net.MalformedURLException; import com.isanta.webServiceTest.TestWebService; public class Test { public static void main(String[] args) throws MalformedURLException, Exception { TestWebService.testWebService(); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值