首先从应用服务器获取验证字符串,然后去支付宝请求验证
private void startAliAuth(final String info) { Runnable authRunnable = new Runnable() { @Override public void run() { AuthTask authTask = new AuthTask(WithdrawApplyActivity.this); Map<String, String> result = authTask.authV2(info, true); Message msg = new Message(); msg.what = 1; msg.obj = result; mHandler.sendMessage(msg); } }; Thread authThread = new Thread(authRunnable); authThread.start(); }
获取验证结果,并上报给应用服务器
private Handler mHandler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true); if (TextUtils.equals(authResult.getResultStatus(), "9000")) { if (authResult.getResultCode().equals("200")) { showProgressDialog("正在绑定账户"); //TODO 将authResult.getResult()上报给应用服务器 } else { DadaToast.showToast(context, "授权失败"); } } else if (authResult.getResultStatus().equals("6001")) { DadaToast.showToast(context, "用户取消"); } else if (authResult.getResultStatus().equals("4000")) { DadaToast.showToast(context, "支付宝异常"); } else if (authResult.getResultStatus().equals("6002")) { DadaToast.showToast(context, "网络连接出错"); } else { DadaToast.showToast(context, "授权失败"); } } } };
完成验证
public class AuthResult { private String resultStatus; private String result; private String memo; private String resultCode; private String authCode; private String alipayOpenId; public AuthResult(Map<String, String> rawResult, boolean removeBrackets) { if (rawResult == null) { return; } for (String key : rawResult.keySet()) { if (TextUtils.equals(key, "resultStatus")) { resultStatus = rawResult.get(key); } else if (TextUtils.equals(key, "result")) { result = rawResult.get(key); } else if (TextUtils.equals(key, "memo")) { memo = rawResult.get(key); } } String[] resultValue = result.split("&"); for (String value : resultValue) { if (value.startsWith("alipay_open_id")) { alipayOpenId = removeBrackets(getValue("alipay_open_id=", value), removeBrackets); continue; } if (value.startsWith("auth_code")) { authCode = removeBrackets(getValue("auth_code=", value), removeBrackets); continue; } if (value.startsWith("result_code")) { resultCode = removeBrackets(getValue("result_code=", value), removeBrackets); continue; } } } private String removeBrackets(String str, boolean remove) { if (remove) { if (!TextUtils.isEmpty(str)) { if (str.startsWith(""")) { str = str.replaceFirst(""", ""); } if (str.endsWith(""")) { str = str.substring(0, str.length() - 1); } } } return str; } @Override public String toString() { return "resultStatus={" + resultStatus + "};memo={" + memo + "};result={" + result + "}"; } private String getValue(String header, String data) { return data.substring(header.length(), data.length()); } /** * @return the resultStatus */ public String getResultStatus() { return resultStatus; } /** * @return the memo */ public String getMemo() { return memo; } /** * @return the result */ public String getResult() { return result; } /** * @return the resultCode */ public String getResultCode() { return resultCode; } /** * @return the authCode */ public String getAuthCode() { return authCode; } /** * @return the alipayOpenId */ public String getAlipayOpenId() { return alipayOpenId; } }