百度,信鸽推送,百度短网址API

来源:互联网 发布:android ndk mac 编辑:程序博客网 时间:2024/04/18 22:18
java  jdbc获取最新插入的数据ID


            PreparedStatement pre=this.getConnection().prepareStatement(insertTagSql);
            pre.execute(insertTagSql, PreparedStatement.RETURN_GENERATED_KEYS);//执行SQL
            ResultSet re=pre.getGeneratedKeys();
             int maxTagId=0;
             while(re.next()){
                   maxTagId=re.getInt(1);//得到当前添加的标签的ID
            }  
     
        java解决get请求字符串String乱码问题:
        String content = new String(request.getParameter("content").getBytes("iso8859-1"),"utf-8"); 
 -----------------------------------------------------------------------------------------------------------------      
百度推送:

先下载打包好的jar

     http://yunpan.cn/lk/cyqPNqdd5ZmmV  提取码 7df9
     
导入项目
          调用代码如下:

     
package com.jeecms.cms.action.push;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.jeecms.cms.Constants;
/**
* android百度云推送
* @author javaweb
*
*/
package com.jeecms.cms.action.push;
import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushBroadcastMessageRequest;
import com.baidu.yun.channel.model.PushBroadcastMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.google.gson.Gson;
import com.jeecms.cms.Constants;
import com.jeecms.cms.entity.push.PushJson;
/**
* android百度云推送
* @author javaweb
*
*/
public class AndroidPush implements Runnable{
     private PushJson json;
    
     public AndroidPush(PushJson json) {
          super();
          this.json = json;
     }

     /**
     * 线程推送
     */
     @Override
     public void run() {   
             // 1. 设置developer平台的ApiKey/SecretKey
             ChannelKeyPair pair = new ChannelKeyPair(Constants.AndroidPush.apiKey,Constants.AndroidPush.secretKey);
             // 2. 创建BaiduChannelClient对象实例
             BaiduChannelClient channelClient = new BaiduChannelClient(pair);
             // 3. 若要了解交互细节,请注册YunLogHandler类
             channelClient.setChannelLogHandler(new YunLogHandler() {
                 @Override
                 public void onHandle(YunLogEvent event) {
                     System.out.println("百度推送返回值L= "+event.getMessage());
                 }
             });
             try {
                 // 4. 创建请求类对象
                 PushBroadcastMessageRequest request = new PushBroadcastMessageRequest();
                 request.setMessageType(0);
                 request.setDeviceType(3);
                 Gson gson=new Gson();
                  String message= gson.toJson(json); 
                  request.setMessage(message);
                 // 5. 调用pushMessage接口
                 PushBroadcastMessageResponse response = channelClient.pushBroadcastMessage(request);
                 if (response.getSuccessAmount() == 1) {
                     // TODO
                      System.err.println("推送验证成功");
                 }else{
                      System.err.println("验证失败#");
                 }
             } catch (ChannelClientException e) {
                 // 处理客户端错误异常
                 e.printStackTrace();
                
             } catch (ChannelServerException e) {
                 // 处理服务端错误异常
                 System.out.println(String.format(
                         "request_id: %d, error_code: %d, error_message: %s",
                         e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
         }
     }
     public static void main(String[] args) {
          PushJson json=new PushJson();
          AndroidPush push=new AndroidPush(json);
          new Thread(push).start();//启动线程
     }
}
 
-------------------------------------------------------------------------------------------
ios推送(采用自带的证书推送,IOS自带推送)

package com.jeecms.cms.action.push;

import java.util.ArrayList;
import java.util.List;

import antlr.Token;

import com.jeecms.cms.Constants;
import com.jeecms.cms.entity.push.Push;
import com.jeecms.cms.util.StringUtils;

import javapns.back.PushNotificationManager;
import javapns.back.SSLConnectionHelper;
import javapns.data.Device;
import javapns.data.PayLoad;
import javapns.data.PayLoadCustomAlert;

/**
* 推送内容
* @author javaweb
*
*/
public class IosPush implements Runnable{  
          private String token;
          private String content;
         
       protected static PushNotificationManager push = PushNotificationManager.getInstance();//ios推送API
      
     public IosPush(String token, String content) {
          super();
          this.token = token;
          this.content = content;
     }

     /**
       * @param path 证书地址
       * @param token  设备token值
       * @param conent 推送的标题
       * @param id 详情ID
       * @param typeId 类型Id
       * @num 推送的数量
       * @throws Exception
       */ 
     @Override
     public void run() {
          try{
               PayLoad payLoad = new PayLoad();//有效载荷 
               String certificatePassword = Constants.Push.certificatePassword.trim();//密码
               String diplomaVersion =Constants.path+Constants.Push.ios_san_box_pwd;//测试证书 
               String host=Constants.Push.sandhost.trim();//推送地址 
               int port = 2195;//端口
              
               PayLoadCustomAlert payLoadCustomAlert = new PayLoadCustomAlert();
              
               List<String> strList = new ArrayList<String>();
              
               payLoadCustomAlert.addLocArgs(strList);
               String tag = "iPhone"+token.substring(1,6);
               payLoad.addAlert(Constants.content.trim());//标题
              
               payLoad.addBadge(StringUtils.parInt(Constants.num));//推送数量
               payLoad.addSound(Constants.Push.defaultStr);
               payLoadCustomAlert.addLocKey(content);// 内容
               payLoad.addCustomDictionary(Constants.Push.idKey,Constants.idvalue); // 详情id
               payLoad.addCustomDictionary(Constants.Push.typeKey, Constants.typevalue); //类型  
               payLoad.addCustomDictionary(Constants.Push.ios_push_title,Constants.ios_title);//推送的标题
               payLoadCustomAlert.addActionLocKey(Constants.Push.buttonTitle);//按钮名称
               payLoadCustomAlert.addBody(Constants.idvalue);//
               payLoad.addCustomAlert(payLoadCustomAlert);    
               push.addDevice(tag, token);//设备token值
               push.initializeConnection(host, port, diplomaVersion, certificatePassword,                           SSLConnectionHelper.KEYSTORE_TYPE_PKCS12);
               Device client = push.getDevice(tag);//推送的设备
               push.sendNotification(client, payLoad);
               push.removeDevice(tag);//移除设备
               push.stopConnection();//停止链接 
          }catch (Exception e) {
               System.err.println("未找到客户端或推送的消息已存在客户端#");
          }
     }
}

     
java list去重复

                                        for ( int i = 0; i < appStart.size(); i++) {
                                                     for ( int j = appStart.size() - 1 ; j > i; j -- ) { 
                                                       if (appStart.get(j).getToken().equals(appStart.get(i).getToken())) { 
                                                               appStart.remove(j); 
                                                       }  
                                                     }     
jdbc获取添加数据的最新ID
                                    String insertTagSql="insert into config_tag (title) values ('"+tagName+ "') "
                                    PreparedStatement pre=this.getConnection().prepareStatement(insertTagSql); 
                                    pre.execute(insertTagSql, PreparedStatement.RETURN_GENERATED_KEYS );//执行SQL
                                    ResultSet re=pre.getGeneratedKeys();
                                     int maxTagId=0;
                                     while(re.next()){
                                                maxTagId=re.getInt(1); //得到当前添加的 标签的ID
                                    }  
 
             /**
             *java 普通类获取webRoot
             * @param args
             */
   public  String getWebRootPath(String mkdirName){
            String pathStr = this.getClass().getClassLoader().getResource( "").getPath();
           // 对 Windows 下获取 物理路径 做 特殊处理
           if( "\\".equals(File. separator)) {
              pathStr = pathStr .substring(1).replaceAll( "/" , "\\\\" );
          }
           String path= pathStr.split( "WEB-INF")[0]+mkdirName+File. separatorChar;
           File file= new File(path);
           if(!file.exists()){ //当文件路径不存在
                       file.mkdir();
           }
           return path;
  }



package com.jeecms.common.util;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
* 生成短网址
* @author 张亮(witt)
* 人生苦短
*2015-6-8
*/
public class GenerateShortUrlUtil {
     public static DefaultHttpClient httpclient;
     static {
          httpclient = new DefaultHttpClient();
     }
    
     /**
     * 生成短网址
     * @param url
     * @return
     */
     public static String  generateShortUrl(String url) {
          try {
               HttpPost httpost = new HttpPost("http://dwz.cn/create.php");
               List<NameValuePair> params = new ArrayList<NameValuePair>();
               params.add(new BasicNameValuePair("url", url)); // 用户名称
               httpost.setEntity(new UrlEncodedFormEntity(params,  "utf-8"));
               HttpResponse response = httpclient.execute(httpost);
               String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
               System.out.println(jsonStr);
               JSONObject object = JSON.parseObject(jsonStr);
               System.out.println(object.getString("tinyurl"));
               return object.getString("tinyurl");
          } catch (Exception e) {
               e.printStackTrace();
               return "Error";
          }
     }
}
0 0
原创粉丝点击