Android短信接口开发经验及具体开发实现

Android、Java使用的短信SDK是多线程:

http://sdk.entinfo.cn:8061/webservice.asmx

其他编程语言使用的是下面接口,希望注意。

http: //sdk.entinfo.cn:8060/webservice.asmx

一、Client.class类定义

package com.example.message;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Client {

	/*
	 * webservice服务器定义
	 */
	private String serviceURL = "http://sdk.entinfo.cn:8061/webservice.asmx";
	//这里面使用多线程接口,而不是http://sdk.entinfo.cn:8060/webservice.asmx
	private String sn = "";// 序列号
	private String password = "";
	private String pwd = "";// 密码

	/*
	 * 构造函数
	 */
	public Client(String sn, String password)
			throws UnsupportedEncodingException {
		this.sn = sn;
		this.password = password;
		//密码为md5(sn+password)
		this.pwd = this.getMD5(sn + password);
	}

	/*
	 * 方法名称:getMD5 
	 * 功    能:字符串MD5加密 
	 * 参    数:待转换字符串 
	 * 返 回 值:加密之后字符串
	 */
	public String getMD5(String sourceStr) throws UnsupportedEncodingException {
		String resultStr = "";
		try {
			byte[] temp = sourceStr.getBytes();
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(temp);
			// resultStr = new String(md5.digest());
			byte[] b = md5.digest();
			for (int i = 0; i < b.length; i++) {
				char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
						'9', 'A', 'B', 'C', 'D', 'E', 'F' };
				char[] ob = new char[2];
				ob[0] = digit[(b[i] >>> 4) & 0X0F];
				ob[1] = digit[b[i] & 0X0F];
				resultStr += new String(ob);
			}
			return resultStr;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}


	/*
	 * 方法名称:mdgetSninfo 
	 * 功    能:获取信息
	 * 参    数:sn,pwd(软件序列号,加密密码md5(sn+password))
	 * 
	 */
	public String mdgetSninfo() {
		String result = "";
		String soapAction = "http://entinfo.cn/mdgetSninfo";
		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		xml += "<soap:Body>";
		xml += "<mdgetSninfo xmlns=\"http://entinfo.cn/\">";
		xml += "<sn>" + sn + "</sn>";
		xml += "<pwd>" + pwd + "</pwd>";
		xml += "<mobile>" + "" + "</mobile>";
		xml += "<content>" + "" + "</content>";
		xml += "<ext>" + "" + "</ext>";
		xml += "<stime>" + "" + "</stime>";
		xml += "<rrid>" + "" + "</rrid>";
		xml += "<msgfmt>" + "" + "</msgfmt>";
		xml += "</mdgetSninfo>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";

		URL url;
		try {
			url = new URL(serviceURL);

			URLConnection connection = url.openConnection();
			HttpURLConnection httpconn = (HttpURLConnection) connection;
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			bout.write(xml.getBytes());
			byte[] b = bout.toByteArray();
			httpconn.setRequestProperty("Content-Length", String
					.valueOf(b.length));
			httpconn.setRequestProperty("Content-Type",
					"text/xml; charset=gb2312");
			httpconn.setRequestProperty("SOAPAction", soapAction);
			httpconn.setRequestMethod("POST");
			httpconn.setDoInput(true);
			httpconn.setDoOutput(true);

			OutputStream out = httpconn.getOutputStream();
			out.write(b);
			out.close();

			InputStreamReader isr = new InputStreamReader(httpconn
					.getInputStream());
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			while (null != (inputLine = in.readLine())) {
				Pattern pattern = Pattern.compile("<mdgetSninfoResult>(.*)</mdgetSninfoResult>");
				Matcher matcher = pattern.matcher(inputLine);
				while (matcher.find()) {
					result = matcher.group(1);
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	
	/*
	 * 方法名称:mdgxsend 
	 * 功    能:发送个性短信 
	 * 参    数:mobile,content,ext,stime,rrid,msgfmt(手机号,内容,扩展码,定时时间,唯一标识,内容编码)
	 * 返 回 值:唯一标识,如果不填写rrid将返回系统生成的
	 */
	public String mdgxsend(String mobile, String content, String ext, String stime,
			String rrid, String msgfmt) {
		String result = "";
		String soapAction = "http://entinfo.cn/mdgxsend";
		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		xml += "<soap:Body>";
		xml += "<mdgxsend xmlns=\"http://entinfo.cn/\">";
		xml += "<sn>" + sn + "</sn>";
		xml += "<pwd>" + pwd + "</pwd>";
		xml += "<mobile>" + mobile + "</mobile>";
		xml += "<content>" + content + "</content>";
		xml += "<ext>" + ext + "</ext>";
		xml += "<stime>" + stime + "</stime>";
		xml += "<rrid>" + rrid + "</rrid>";
		xml += "<msgfmt>" + msgfmt + "</msgfmt>";
		xml += "</mdgxsend>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";

		URL url;
		try {
			url = new URL(serviceURL);

			URLConnection connection = url.openConnection();
			HttpURLConnection httpconn = (HttpURLConnection) connection;
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			bout.write(xml.getBytes());
			byte[] b = bout.toByteArray();
			httpconn.setRequestProperty("Content-Length", String
					.valueOf(b.length));
			httpconn.setRequestProperty("Content-Type",
					"text/xml; charset=gb2312");
			httpconn.setRequestProperty("SOAPAction", soapAction);
			httpconn.setRequestMethod("POST");
			httpconn.setDoInput(true);
			httpconn.setDoOutput(true);

			OutputStream out = httpconn.getOutputStream();
			out.write(b);
			out.close();

			InputStreamReader isr = new InputStreamReader(httpconn
					.getInputStream());
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			while (null != (inputLine = in.readLine())) {
				Pattern pattern = Pattern.compile("<mdgxsendResult>(.*)</mdgxsendResult>");
				Matcher matcher = pattern.matcher(inputLine);
				while (matcher.find()) {
					result = matcher.group(1);
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	
	/*
	 * 方法名称:mdsmssend
	 * 功    能:发送短信 
	 * 参    数:mobile,content,ext,stime,rrid,msgfmt(手机号,内容,扩展码,定时时间,唯一标识,内容编码)
	 * 返 回 值:唯一标识,如果不填写rrid将返回系统生成的
	 */
	public String mdsmssend(String mobile, String content, String ext, String stime,
			String rrid,String msgfmt) {
		String result = "";
		String soapAction = "http://entinfo.cn/mdsmssend";
		String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
		xml += "<soap:Body>";
		xml += "<mdsmssend  xmlns=\"http://entinfo.cn/\">";
		xml += "<sn>" + sn + "</sn>";
		xml += "<pwd>" + pwd + "</pwd>";
		xml += "<mobile>" + mobile + "</mobile>";
		xml += "<content>" + content + "</content>";
		xml += "<ext>" + ext + "</ext>";
		xml += "<stime>" + stime + "</stime>";
		xml += "<rrid>" + rrid + "</rrid>";
		xml += "<msgfmt>" + msgfmt + "</msgfmt>";
		xml += "</mdsmssend>";
		xml += "</soap:Body>";
		xml += "</soap:Envelope>";

		URL url;
		try {
			url = new URL(serviceURL);

			URLConnection connection = url.openConnection();
			HttpURLConnection httpconn = (HttpURLConnection) connection;
			ByteArrayOutputStream bout = new ByteArrayOutputStream();
			bout.write(xml.getBytes());
			byte[] b = bout.toByteArray();
			httpconn.setRequestProperty("Content-Length", String
					.valueOf(b.length));
			httpconn.setRequestProperty("Content-Type",
					"text/xml; charset=gb2312");
			httpconn.setRequestProperty("SOAPAction", soapAction);
			httpconn.setRequestMethod("POST");
			httpconn.setDoInput(true);
			httpconn.setDoOutput(true);

			OutputStream out = httpconn.getOutputStream();
			out.write(b);
			out.close();

			InputStreamReader isr = new InputStreamReader(httpconn
					.getInputStream());
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			while (null != (inputLine = in.readLine())) {
				Pattern pattern = Pattern.compile("<mdsmssendResult>(.*)</mdsmssendResult>");
				Matcher matcher = pattern.matcher(inputLine);
				while (matcher.find()) {
					result = matcher.group(1);
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
}


 

二、普通群发短信mdsmssend

 

参数名称

说明

是否必须有值

备注

sn

软件序列号

格式XXX-XXX-XXX-XXXXX

pwd

密码

md5(sn+password) 32位大写密文

mobile

手机号

必填(支持10000个手机号,建议<=5000)多个英文逗号隔开

Content

内容

支持长短信(详细请看长短信扣费说明),URLUTF8编码

Ext

扩展码

例如:123(默认置空)

stime

定时时间

例如:2010-12-29 16:27:03(置空表示立即发送)

Rrid

唯一标识

最长18位,支持数字。

msgfmt
内容编码
0:ASCII串。3:短信写卡操作。4:二进制信息。空或15:含GB汉字

函数返回值:String(唯一标识,即当前发送短信批次的唯一标识,和rrid对应,如为空则返回系统生成的rrid)

sn即您注册时的序列号,pwd需要MD5(SN+pwd)加密,取32位大写。

接口地址:http://sdk.entinfo.cn:8061/webservice.asmx?op=mdsmssend

示例1

SN= SDK-SSD-010-00001

PWD=3B5D3C427365F40C1D27682D78BB31E0

Mobile:139***404,138***213…………….

Content:测试

Ext: ""

Stime: ""

Rrid: ""

输出结果:

XML格式:

具体Android调用实现:

package com.example.message;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import android.os.Bundle;  
import android.support.v4.app.Fragment;  
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.example.message.Client;
public class sendmessage   extends Fragment 
{
		String result2="123132";
	    @Override  
	    public void onCreate(Bundle savedInstanceState)
	    {  
	        // TODO Auto-generated method stub  
	        super.onCreate(savedInstanceState);  
	        android.util.Log.d("mark", "onCreate()--------->news Fragment"); 
	        
	    }  
	    @Override  
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	    {  
	        // TODO Auto-generated method stub  
	        View view = inflater.inflate(R.layout.sendmessage, null);  
	        android.util.Log.d("mark", "onCreateView()--------->news Fragment");  
	        return view;  
	    }  
	    @Override  
	    public void onPause() 
	    {  
	        // TODO Auto-generated method stub  
	        super.onPause();  
	        android.util.Log.d("mark", "onPause()--------->news Fragment");  
	    }  
	    public void onActivityCreated(Bundle savedInstanceState)
	    {
	    	 super.onActivityCreated(savedInstanceState);
	    	 System.out.println("BBBBBBBBBBB____onActivityCreated");
             final EditText editText =  (EditText)this.getView().findViewById(R.id.editText1);
             this.getView().findViewById(R.id.button1).setOnClickListener(
				 new View.OnClickListener()
				 {
					 @Override
					 public void onClick(View view)
					 {
						 // 获得绑定的FragmentActivity
						 MainActivity activity = ((MainActivity)getActivity());
						 //	String result = client.mdgetSninfo();
						 //System.out.print(result);
						 // 获得TabAFm的控件
						 // String result_mt = client.mdsmssend("", content, "", "", "", "");
						 //	String test=SendThread.
						 new Thread(downloadRun).start(); 
						 editText.setText(result2);
						 //Toast.makeText(activity, result2 , Toast.LENGTH_SHORT).show();
					 }
				 }
             );
        }
	    Runnable downloadRun = new Runnable()
	    { 
			@Override 
			public void run() 
			{ 
				// TODO Auto-generated method stub 
			  	Client client = null;
			    final String sn="SDK-SSD-010-00001";
	          	final String pwd="xxxxxx";
	         
				try 
				{
					  client=new Client(sn,pwd);
				} 
				catch (UnsupportedEncodingException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//SendThread dTask = new SendThread();
				//  dTask.execute(1);
				String content = null;
				try
				{
					content = URLEncoder.encode("您的验证码887766已经发出,请及时输入,本条测试【雷雨科技】", "utf8");
				} 
				catch (UnsupportedEncodingException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				result2="test";
				result2 = client.mdsmssend("18636924700", content, "", "", "", "");
			 } 
		}; 
}


 

三、webservice返回集合对照表:

 

返回值

返回值说明

问题描述

-2 

帐号/密码不正确

1.序列号未注册2.密码加密不正确3.密码已被修改4.序列号已注销

-4

余额不足支持本次发送(或者修改密码长度不正确)

余额不足(或者修改密码长度不在6位到10位之间)

-5

数据格式错误

只能自行调试了。或与技术支持联系

-6

参数有误

看参数传的是否均正常,请调试程序查看各参数

-7

权限受限

该序列号是否已经开通了调用该方法的权限

-8

流量控制错误

-9

扩展码权限错误

该序列号是否已经开通了扩展子号的权限,把ext这个参数置空。

-10

内容长度长

单字节不能超过1000个字符,双字节不能超过500个字符

-11

内部数据库错误

-12

序列号状态错误

序列号是否被禁用

-14

服务器写文件失败

-17

没有权限

如发送彩信仅限于SDK3

-19

禁止同时使用多个接口地址

每个序列号提交只能使用一个接口地址

-20

相同手机号,相同内容重复提交

-22

Ip鉴权失败

提交的IP不是所绑定的IP

-23

缓存无此序列号信息

-601

序列号为空,参数错误

-602

序列号格式错误,参数错误

-603

密码为空,参数错误

-604

手机号码为空,参数错误

-605

内容为空,参数错误

-606

ext长度大于9,参数错误

-607

参数错误 扩展码非数字 

-608

参数错误 定时时间非日期格式

-609

rrid长度大于18,参数错误 

-610

参数错误 rrid非数字

-611

参数错误 内容编码不符合规范

-623

手机个数与内容个数不匹配

-624

扩展个数与手机个数数

-644 

rrid个数与手机个数不一致

注:以上返回值针对个别方法.请具体参看每个用到方法的详细说明。

四、附加说明:

1.接口地址:

常用接口地址:http://sdk.entinfo.cn:8060/webservice.asmx (一般调用)

多线程接口地址:http://sdk.entinfo.cn:8061/webservice.asmx (java、andriod使用)

2.其它说明:

(1)开发使用的帐号必须为SDK开头,如SDK-SSD-010-00001,帐号第一次需要调用Register方法注册一次.仅需注册一次即可,信息必须真实

(2)UnRegister与Register配合使用, 连续使用不得超过10次/天;

(3)群发推荐使用接口方法 mt或者mdSmsSend (仅方法名不同);

3. 郑重声明:

(1)禁止相同的内容多个手机号连续一条一条提交. 否则禁用帐号,由此带来损失由客户自行负责.

(2)请客户提供外网服务器IP以便于绑定IP发送,提高账号的安全性!

(3)在程序里最好有配置文件,程序自动判断当某个接口连接超时提交速度变慢时.程序可以自动切换其它的接口以下是推荐的几个服务器,仅接口地址不同而已.方法全部相同;

地址1:http://sdk.entinfo.cn:8060/webservice.asmx

地址2:http://sdk2.entinfo.cn:8060/webservice.asmx

这些地址都是标准的webservice地址,C#,Java客户可以按照自己熟悉的方式去解析String   

或者

地址1:http://sdk.entinfo.cn:8060/webservice.asmx?wsdl

地址2:http://sdk2.entinfo.cn:8060/webservice.asmx?wsdl

五、示例Demo源代码下载:

DEMO  SDK通用版接口文档  所有下载

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值