Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 銀聯支付接入報nullexception異常

android 銀聯支付接入報nullexception異常

編輯:關於Android編程

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);
   }


}

 

 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved