java移动公司计算话费_话费充值⽰例代码
话费充值
package bilerecharge;
/**
* 话费充值
*/
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import Decoder.BASE64Encoder;
import api.util.HttpUtil;
import net.sf.json.JSONObject;
public class Recharge {
public static final String APPKEY = "your_appkey_here";// 你的appkey
public static final String URL = "api.binstd/mobilerecharge/recharge";
public static final String mobile = "151********";// ⼿机号
public static final String amount = "30";// 充值⾦额
public static final String outorderno = "66522311111";// 外部订单号
public static final String appsecret = "sssssssssss";
public static Mapqueryarr;
public static void Get() throws NoSuchAlgorithmException, UnsupportedEncodingException {
queryarr = new HashMap();
queryarr.put("amount", amount);
queryarr.put(mobile, mobile);
queryarr.put("outorderno", outorderno);
String sign = makeSign(queryarr, appsecret);// 签名 mobile amount
// outorderno为签名字段,参考下⾯的签名函数
String result = null;
String url = URL + "?appkey=" + APPKEY + "&mobile=" + mobile + "&amount=" + amount + "&outorderno=" + outorderno + "&sign=" + sign;
try {
result = HttpUtil.sendGet(url, "utf-8");
JSONObject json = JSONObject.fromObject(result);
if (Int("status") != 0) {
System.out.String("msg"));
} else {
JSONObject resultarr = (JSONObject) json.opt("result");
String mobile = String("mobile");
String totalfee = String("totalfee");
String amount = String("amount");
String outorderno = String("outorderno");
String orderno = String("orderno");
String rechargestatus = String("rechargestatus");
System.out.println(mobile + " " + amount + " " + outorderno + " " + orderno + " " + totalfee + " " + rechargestatus);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String makeSign(Mapqueryarr, String appsecret)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
TreeMap map = new TreeMap(queryarr);
Iterator ir = map.keySet().iterator();
String str = new String();
while (ir.hasNext()) {
Object key = ir.next();
str += (key);
}
str += appsecret;
return Bytes());
}
public static String getMd5(byte[] buffer) throws NoSuchAlgorithmException {
String s = null;
char hexDigist[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; MessageDigest md = Instance("MD5");
md.update(buffer);
byte[] datas = md.digest(); // 16个字节的长整数
char[] str = new char[2 * 16];
int k = 0;
for (int i = 0; i < 16; i++) {
byte b = datas[i];
str[k++] = hexDigist[b >>> 4 & 0xf];// ⾼4位
str[k++] = hexDigist[b & 0xf];// 低4位
}
s = new String(str);
移动网上充值话费
return s;
}
}