【君贤短信】取回上行短信

package com.gdcy.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.log4j.Logger;

public class SmsRequestHandler {
	protected final static Logger log = Logger.getLogger(SmsRequestHandler.class);
	public static void smsHandle(String responseString) {
		if(responseString != null && !responseString.trim().equals("")){
			if(responseString.indexOf("/") > -1){
				String returnMessage = responseString.substring(0, responseString.indexOf("/"));
				if(returnMessage != null && !returnMessage.trim().equals("")){
					if(returnMessage.trim().equalsIgnoreCase("ERROR")){
						String errorReason = responseString.substring(responseString.indexOf("/")+1);
						//System.out.println("errorReason = "+errorReason);
						if(errorReason != null && errorReason.trim().equals("无回复信息")){
							//to do
						}else if(errorReason != null && errorReason.trim().equals("账号、密码错误")){
							log.error("【君贤短信】取回上行短信的账号、密码错误 ");
						}else{
							log.error("【君贤短信】取回上行短信未知错误原因: "+errorReason);
						}
					}else if(returnMessage.trim().equalsIgnoreCase("OK")){
						//返回数据格式:OK/2007-1-1%%13:15:07%%13602683188%%活动安排在什么地方?$$2007-1-1%%13:18:31%%13924786445%%我决定不参加。$$
						String allResult = responseString.substring(responseString.indexOf("/")+1);
						if(allResult != null && !allResult.trim().equals("")){
							Connection conn = null;
					        PreparedStatement preparedStatement = null;
							try {
								if(allResult.indexOf("$$") < 0){
									log.error("【君贤短信】返回的数据格式不正确,找不到$$。");
								}else{
									String[] results = allResult.split("\\$\\$");
									if(results != null && results.length > 0){
										String driver = "com.mysql.jdbc.Driver";   
								        String url = "jdbc:mysql://127.0.0.1:3306/sms";   
								        String user = "root";   
								        String password = "123456"; 
								        String preparedSql = "INSERT INTO `upsms`

(id,strDate,strTime,phone,content,receiveTime,`status`,`version`,createdTime) VALUES (?,?,?,?,?,?,?,?,?)";
								        Class.forName(driver);
								        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
								        conn = DriverManager.getConnection(url, user, password);
							            conn.setAutoCommit(false);
								        preparedStatement = conn.prepareStatement(preparedSql);
								        for (String result : results) {
											if(result != null && !result.trim().equals("")){
												if(result.indexOf("%%") < 0){
													log.error("【君贤短信】返回的数据格式不正确,找不到%%。");
												}else{
													//2007-1-1%%13:15:07%%13602683188%%活动安排在什么地方?
													String[] datas = result.split("\\%\\%");
													if(datas != null && datas.length > 3){
														String dataDate = datas[0];
														String dataTime = datas[1];
														String dataPhone = datas[2];
														String dataContent = datas[3];
														/*System.out.println("dataDate = "+dataDate);
														System.out.println("dataTime = "+dataTime);
														System.out.println("dataPhone = "+dataPhone);
														System.out.println("dataContent = "+dataContent);*/
											            if (!conn.isClosed()) {
											            	preparedStatement.setString(1, UUID.randomUUID().toString());
											                preparedStatement.setString(2, dataDate);
											                preparedStatement.setString(3, dataTime);
											                preparedStatement.setString(4, dataPhone);
											                preparedStatement.setString(5, dataContent);
															Date receiveTime = dateFormat.parse(dataDate+" 

"+dataTime);
											                preparedStatement.setDate(6, new java.sql.Date(receiveTime.getTime()));
											                preparedStatement.setInt(7, 0);
											                preparedStatement.setInt(8, 0);
											                preparedStatement.setDate(9, new java.sql.Date(new Date().getTime()));
											                preparedStatement.addBatch();
											            }
													}else{
														log.error("【君贤短信】返回的数据格式不正确,%%拆分后数量少于4个

。");
													}
												}
											}
										}
										preparedStatement.executeBatch();
										conn.commit();
										preparedStatement.clearBatch();
									}else{
										log.error("【君贤短信】返回的数据格式不正确。");
									}
								}
							} catch (SQLException e) {
								e.printStackTrace();
							} catch (ParseException e) {
								e.printStackTrace();
							} catch (ClassNotFoundException e) {
								e.printStackTrace();
							}finally{
								try {
									if(preparedStatement != null){
										preparedStatement.close();
										preparedStatement = null;
									}
									if(conn != null){
										conn.close();
										conn = null;
									}
								} catch (SQLException e) {
									e.printStackTrace();
								}
							}
						}
					}else{
						//ERROR/账号、密码错误 
						log.error("【君贤短信】返回未确定结果:"+responseString);
					}
				}
			}else{
				//返回的数据格式不正确
				log.error("【君贤短信】返回的数据格式不正确,找不到/。");
			}
		}
	}

}

 

【君贤短信】取回上行短信操作类SmsRequestHandler,想要再好就要封装数据库操作了。

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

蕃薯耀分享 2013年5月8日 20:57:10 星期三

 

你可能感兴趣的:(君贤短信,【君贤短信】取回上行短信)