package com.example.ex_example; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Handler; import android.os.Message; import android.util.Log; import com.unionpay.UPPayAssistEx; import com.unionpay.uppay.PayActivity; public class UnionPay implements Runnable{ // public static final int PLUGIN_VALID = 0; // public static final int PLUGIN_NOT_INSTALLED = -1; // public static final int PLUGIN_NEED_UPGRADE = 2; /***************************************************************** * mMode参数解释: "00" - 启动银联正式环境 "01" - 连接银联测试环境 测试用 卡号:6226440123456785 * 密码:111101 *****************************************************************/ private final static String mMode = "01"; private static final String TN_URL_01 = "http://202.101.25.178:8080/sim/gettn"; private static ProgressDialog mLoadingDialog = null; private static Activity activity; private static UnionPay unionPay = new UnionPay(); public static void unionpay(Activity activity) { UnionPay.activity = activity; mLoadingDialog = ProgressDialog.show(activity, // context "", // title "正在努力的获取tn中,请稍候...", // message true); // 进度是否是不确定的,这只和创建进度条有关 /************************************************* * 步骤1:从网络开始,获取交易流水号即TN ************************************************/ new Thread(unionPay).start(); // //支付金额 订单金额 // String orderAmount = "1533"; // //千引商户号 // String merchantNO = "805920100000069"; // //商户订单号 // String orderNO = "467934311"; // //通知Url // String backUrl = "http://www.baidu.com"; // //签名密钥 // String key = "86Aab696Fed90bc691a7AfE0b6a269Ac"; // //签名字段 // String signcont = // merchantNO+"&"+orderNO+"&"+orderAmount+"&"+backUrl+"&"+key; // // Log.e("", ""+signcont); // // String sign = MD5.digest(signcont); // // String sign = MD5Utils.generate(signcont); // Log.e("", "sign = "+sign.length()); // String url = "http://mpay.sinoqy.com:6283/qyapi/trans/getTn" // + "?orderAmount="+orderAmount // + "&merchantNO="+merchantNO // + "&orderNO="+ orderNO // + "&backUrl="+backUrl // + "&Sign="+ sign; // // Log.e("", ""+url); // // //获取TN // HTTPUtils.get(this, url , new VolleyListener() { // // @Override // public void onErrorResponse(VolleyError arg0) { // Log.e("", "VolleyError= "+arg0); // } // // @Override // public void onResponse(String arg0) { // Log.e("", "onResponse= "+arg0); // } // }); // } // 银联 private static Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { if (mLoadingDialog.isShowing()) { mLoadingDialog.dismiss(); } String tn = ""; if (msg.obj == null || ((String) msg.obj).length() == 0) { AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setTitle("错误提示"); builder.setMessage("网络连接失败,请重试!"); builder.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } else { tn = (String) msg.obj; /************************************************* * 步骤2:通过银联工具类启动支付插件 ************************************************/ doStartUnionPayPlugin(activity, tn, mMode); } } }; // 银联 public static void doStartUnionPayPlugin(Activity activity, String tn, String mode) { Log.e("", "handleMessage"); // TODO // orderInfo ——订单信息为交易流水号,即TN。 // mode —— 银联后台环境标识,“00”将在银联正式环境发起交易, “01”将在 // 银联测试环境发起交易 UPPayAssistEx.startPayByJAR(activity, PayActivity.class, null, null, tn, mode); } @Override public void run() { Log.e("", "run"); String tn = null; InputStream is; try { String url = TN_URL_01; URL myURL = new URL(url); URLConnection ucon = myURL.openConnection(); ucon.setConnectTimeout(120000); is = ucon.getInputStream(); int i = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((i = is.read()) != -1) { baos.write(i); } tn = baos.toString(); is.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } Message msg = mHandler.obtainMessage(); msg.obj = tn; mHandler.sendMessage(msg); } }
这是我自己整合的银联支付,在
package com.example.ex_example; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ //调用 UnionPay.unionpay(this); } }
出现如下异常:
找了N久才找到问题:(该死的这文件没考)
希望大家注意。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。