package com.partynetwork.iparty.app.api;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.JsonToken;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.partynetwork.dataprovider.util.L;
import com.partynetwork.dataprovider.util.MD5Util;
import com.partynetwork.dataprovider.util.StringUtil;
import com.partynetwork.iparty.app.AppConfig;
import com.partynetwork.iparty.app.AppContext;
import com.partynetwork.iparty.app.AppException;
import com.partynetwork.iparty.app.bean.BLogin;
import com.partynetwork.iparty.app.bean.BUpdate;
import com.partynetwork.iparty.app.bean.IpartyList;
import com.partynetwork.iparty.app.bean.NoticeList;
import com.partynetwork.iparty.app.bean.ShareList;
import com.partynetwork.iparty.app.util.StringUtils;
import com.partynetwork.iparty.info.UserInfo;
public class ApiClient {
public static final String UTF_8 = "UTF-8";
public static final String TEXT_HTML = "text/html";
private final static int TIMEOUT_CONNECTION = 20000;
private final static int TIMEOUT_SOCKET = 30000;
private final static int RETRY_TIME = 3;
private static HttpClient getHttpClient() {
HttpClient httpClient = new HttpClient();
// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
httpClient.getParams().setCookiePolicy(
CookiePolicy.BROWSER_COMPATIBILITY);
// 设置 默认的超时重试处理策略
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// 设置 连接超时时间
httpClient.getHttpConnectionManager().getParams()
.setConnectionTimeout(TIMEOUT_CONNECTION);
// 设置 读数据超时时间
httpClient.getHttpConnectionManager().getParams()
.setSoTimeout(TIMEOUT_SOCKET);
// 设置 字符集
httpClient.getParams().setContentCharset(UTF_8);
return httpClient;
}
private static GetMethod getHttpGet(String url) {
GetMethod httpGet = new GetMethod(url);
// 设置 请求超时时间
httpGet.getParams().setSoTimeout(TIMEOUT_SOCKET);
return httpGet;
}
private static PostMethod getHttpPost(String url) {
PostMethod httpPost = new PostMethod(url);
// 设置 请求超时时间
httpPost.getParams().setSoTimeout(TIMEOUT_SOCKET);
return httpPost;
}
private static String _MakeURL(String p_url, Map<String, Object> params) {
StringBuilder url = new StringBuilder(p_url);
if (url.indexOf("?") < 0)
url.append('?');
for (String name : params.keySet()) {
url.append('&');
url.append(name);
url.append('=');
url.append(String.valueOf(params.get(name)));
// 不做URLEncoder处理
// url.append(URLEncoder.encode(String.valueOf(params.get(name)),
// UTF_8));
}
return url.toString().replace("?&", "?");
}
private static InputStream _post(String url, String jsonStr)
throws AppException {
// LogUtils.i("请求url:" + url);
// LogUtils.i("发送的json:" + jsonStr);
HttpClient httpClient = null;
PostMethod httpPost = null;
RequestEntity requestEntity = null;
String responseBody = null;
int time = 0;
do {
try {
httpClient = getHttpClient();
httpPost = getHttpPost(url);
requestEntity = new StringRequestEntity(jsonStr, TEXT_HTML,
UTF_8);
httpPost.setRequestEntity(requestEntity);
int statusCode = httpClient.executeMethod(httpPost);
if (statusCode != HttpStatus.SC_OK) {
throw AppException.http(statusCode);
} else if (statusCode == HttpStatus.SC_OK) {
}
InputStream inputStream = httpPost.getResponseBodyAsStream();
responseBody = StringUtil.convertStreamToString(inputStream);
// LogUtils.i("返回的json:" + responseBody);
// System.out.println("XMLDATA=====>"+responseBody);
break;
} catch (HttpException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
throw AppException.http(e);
} catch (IOException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
throw AppException.network(e);
} finally {
// 释放连接
httpPost.releaseConnection();
httpClient = null;
}
} while (time < RETRY_TIME);
return new ByteArrayInputStream(responseBody.getBytes());
}
/**
* get请求URL
*
* @param url
* @throws AppException
*/
private static InputStream _get(String url) throws AppException {
// System.out.println("get_url==> "+url);
HttpClient httpClient = null;
GetMethod httpGet = null;
String responseBody = "";
int time = 0;
do {
try {
httpClient = getHttpClient();
httpGet = getHttpGet(url);
int statusCode = httpClient.executeMethod(httpGet);
if (statusCode != HttpStatus.SC_OK) {
throw AppException.http(statusCode);
}
responseBody = httpGet.getResponseBodyAsString();
// System.out.println("XMLDATA=====>"+responseBody);
break;
} catch (HttpException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
throw AppException.http(e);
} catch (IOException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
throw AppException.network(e);
} finally {
// 释放连接
httpGet.releaseConnection();
httpClient = null;
}
} while (time < RETRY_TIME);
return new ByteArrayInputStream(responseBody.getBytes());
}
/**
* 公用post方法
*
* @param url
* @param params
* @param files
* @throws AppException
*/
private static InputStream _post(String url, Map<String, Object> params,
Map<String, File> files) throws AppException {
// System.out.println("post_url==> "+url);
HttpClient httpClient = null;
PostMethod httpPost = null;
// post表单参数处理
int length = (params == null ? 0 : params.size())
+ (files == null ? 0 : files.size());
Part[] parts = new Part[length];
int i = 0;
if (params != null) {
for (String name : params.keySet()) {
parts[i++] = new StringPart(name, String.valueOf(params
.get(name)), UTF_8);
// System.out.println("post_key==> "+name+" value==>"+String.valueOf(params.get(name)));
}
}
if (files != null) {
for (String file : files.keySet()) {
try {
parts[i++] = new FilePart(file, files.get(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// System.out.println("post_key_file==> "+file);
}
}
String responseBody = "";
int time = 0;
do {
try {
httpClient = getHttpClient();
httpPost = getHttpPost(url);
httpPost.setRequestEntity(new MultipartRequestEntity(parts,
httpPost.getParams()));
int statusCode = httpClient.executeMethod(httpPost);
if (statusCode != HttpStatus.SC_OK) {
// throw AppException.http(statusCode);
} else if (statusCode == HttpStatus.SC_OK) {
}
InputStream inputStream = httpPost.getResponseBodyAsStream();
responseBody = StringUtil.convertStreamToString(inputStream);
// System.out.println("XMLDATA=====>"+responseBody);
break;
} catch (HttpException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
// throw AppException.http(e);
} catch (IOException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
// throw AppException.network(e);
} finally {
// 释放连接
httpPost.releaseConnection();
httpClient = null;
}
} while (time < RETRY_TIME);
return new ByteArrayInputStream(responseBody.getBytes());
}
/**
* 获取网络图片
*
* @param url
* @return
*/
public static Bitmap getNetBitmap(String url) throws AppException {
// System.out.println("image_url==> "+url);
HttpClient httpClient = null;
GetMethod httpGet = null;
Bitmap bitmap = null;
int time = 0;
do {
try {
httpClient = getHttpClient();
httpGet = getHttpGet(url);
int statusCode = httpClient.executeMethod(httpGet);
if (statusCode != HttpStatus.SC_OK) {
throw AppException.http(statusCode);
}
InputStream inStream = httpGet.getResponseBodyAsStream();
bitmap = BitmapFactory.decodeStream(inStream);
inStream.close();
break;
} catch (HttpException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生致命的异常,可能是协议不对或者返回的内容有问题
e.printStackTrace();
throw AppException.http(e);
} catch (IOException e) {
time++;
if (time < RETRY_TIME) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
continue;
}
// 发生网络异常
e.printStackTrace();
throw AppException.network(e);
} finally {
// 释放连接
httpGet.releaseConnection();
httpClient = null;
}
} while (time < RETRY_TIME);
return bitmap;
}
/**
* 组装最终的请求json
*
* @param context
* @param treeNode
* @return
*/
private static String getRequestJson(Context context, ObjectNode treeNode) {
treeNode.put("ipartyCode", AppConfig.getAppConfig(context)
.getIpartyCode());
return treeNode.toString();
}
/**
* 过滤密匙不正确异常
*
* @param context
* @param e
* @return
* @throws AppException
*/
private static boolean filterException(Context context, AppException e)
throws AppException {
if (e != null && e.getMsg() != null && e.getMsg().contains("密匙")) {
try {
return getIpartyCode(context);
} catch (AppException ex) {
throw ex;
}
} else {
throw e;
}
}
/**
* 获取秘钥
*
* @param context
* @return
* @throws AppException
*/
public static boolean getIpartyCode(Context context) throws AppException {
// url
String url = URLs.IPARTY_CODE;
InputStream stream = _get(url);
String str = StringUtil.convertStreamToString(stream);
if (!StringUtils.isEmpty(str)) {
AppConfig.getAppConfig(context).setIpartyCode(str);
return true;
} else {
return false;
}
}
/**
* 传文件
*/
public static List<String> upFiles4Url(AppContext appContext,
List<File> files) throws AppException {
Map<String, File> fs = new HashMap<String, File>();
for (int i = 0; i < files.size(); i++) {
fs.put("amr" + i, files.get(i));
}
String url = URLs.FILE_UP;
try {
InputStream inputStream = _post(url, null, fs);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
// 成功
JsonNode dataNode = rootNode.path("details");
List<String> list = new ArrayList<String>();
JsonFactory f = new JsonFactory();
JsonParser jp = f.createJsonParser(dataNode.toString());
jp.nextToken();
while (jp.nextToken() != JsonToken.END_ARRAY) {
String info = jp.getText();
list.add(info);
}
return list;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return upFiles4Url(appContext, files);
} else {
throw (AppException) e;
}
}
}
/**
* 检查版本更新
*
* @param url
* @return
*/
public static BUpdate checkVersion(AppContext appContext, String version,
String os) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("version", version);
treeNode.put("os", os);
// url
String url = URLs.UPDATE_VERSION;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
return BUpdate.parse(_post(url, requestJson));
} catch (AppException e) {
if (filterException(appContext, e)) {
return checkVersion(appContext, version, os);
} else {
throw (AppException) e;
}
}
}
/**
* 登录, 自动处理cookie
*
* @param url
* @param username
* @param pwd
* @return
* @throws AppException
*/
public static BLogin login(AppContext appContext, String account,
String pwd, int type) throws AppException {
AppConfig config = AppConfig.getAppConfig(appContext);
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("loginType", type);
treeNode.put("loginContent", account);
treeNode.put("password", MD5Util.md5(pwd));
treeNode.put("baiduUserId", config.getBaiduUserId());
treeNode.put("baiduChannel", config.getBaiduChannel());
treeNode.put("osType", 3);
// url
String url = URLs.LOGIN;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
return BLogin.parse(_post(url, requestJson));
} catch (AppException e) {
if (filterException(appContext, e)) {
return login(appContext, account, pwd, type);
} else {
throw e;
}
}
}
/**
* 获取partyList数据
*
* @param appContext
* @param type
* @param pageNum
* @param lastId
* @param pageSize
* @return
*/
public static IpartyList getIpartyList(AppContext appContext, int type,
int pageNum, int lastId, int pageSize) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("ipartyType", type);
treeNode.put("pageNumber", pageNum);
treeNode.put("pageSize", pageSize);
treeNode.put("lastId", lastId);
// url
String url = URLs.PARTY_LIST;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
return IpartyList.parse(_post(url, requestJson));
} catch (AppException e) {
if (filterException(appContext, e)) {
return getIpartyList(appContext, type, pageNum, lastId,
pageSize);
} else {
throw e;
}
}
}
/**
* 获得i分享列表
*
* @param appContext
* @param type
* @param pageNum
* @param lastId
* @param pageSize
* @return
* @throws AppException
*/
public static ShareList getShareList(AppContext appContext, int type,
int pageNum, int lastId, int pageSize) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("ishareType", type);
treeNode.put("pageNumber", pageNum);
treeNode.put("pageSize", pageSize);
treeNode.put("lastId", lastId);
// url
String url = URLs.SHARE_LIST;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
return ShareList.parse(_post(url, requestJson));
} catch (AppException e) {
if (filterException(appContext, e)) {
return getShareList(appContext, type, pageNum, lastId, pageSize);
} else {
throw e;
}
}
}
/**
* 获得通知列表
*
* @param appContext
* @return
* @throws AppException
*/
public static NoticeList getNoticeList(AppContext appContext)
throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
// url
String url = URLs.MESSAGE_NOTICE;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
return NoticeList.parse(_post(url, requestJson));
} catch (AppException e) {
if (filterException(appContext, e)) {
return getNoticeList(appContext);
} else {
throw e;
}
}
}
/**
* 得到个人信息
*
* @param appContext
* @param userId
* @return
* @throws AppException
*/
public static UserInfo getUserInfo(AppContext appContext, int userId)
throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", userId);
// url
String url = URLs.USER_INFO;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
// 成功
JsonNode dataNode = rootNode.path("details");
UserInfo info = om.readValue(dataNode, UserInfo.class);
return info;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return getUserInfo(appContext, userId);
} else {
throw (AppException) e;
}
}
}
/**
* 获得城市的party数据列表
*
* @param appContext
* @param ipartyType
* @param cityNum
* @return
* @throws AppException
*/
public static IpartyList getIpartyList4City(AppContext appContext,
int ipartyType, String cityNum) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("ipartyType", ipartyType);
treeNode.put("cityNum", cityNum);
// url
String url = URLs.PARTY_CITY_LIST;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
return IpartyList.parse(_post(url, requestJson));
} catch (AppException e) {
if (filterException(appContext, e)) {
return getIpartyList4City(appContext, ipartyType, cityNum);
} else {
throw e;
}
}
}
/**
* 发送打招呼
*
* @param appContext
* @param toUserId
* @param message
* @return
* @throws AppException
*/
public static int setGreet(AppContext appContext, int toUserId,
String message) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("fromUserId", appContext.getLoginUid());
treeNode.put("toUserId", toUserId);
treeNode.put("greetMessage", message);
// url
String url = URLs.PERSONAL_GREET;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return setGreet(appContext, toUserId, message);
} else {
throw (AppException) e;
}
}
}
/**
* 设定账户信息
*
* @param appContext
* @param idCard
* @param question
* @param answer
* @param password
* @return
* @throws AppException
*/
public static int setAccountInfo(AppContext appContext, String idCard,
String question, String answer, String password)
throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("idNumber", idCard);
treeNode.put("passwordProtection", question);
treeNode.put("passwordProtectionAnswer", answer);
treeNode.put("paymentPassword", MD5Util.md5(password));
// url
String url = URLs.ACCOUNT_SET_ACCOUNTINFO;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return setAccountInfo(appContext, idCard, question, answer,
password);
} else {
throw (AppException) e;
}
}
}
/**
* 重设支付密码
*
* @param appContext
* @param idCard
* @param question
* @param answer
* @return
*/
public static int setPaymentPassword(AppContext appContext, String idCard,
String question, String answer, String password)
throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("idNumber", idCard);
treeNode.put("passwordProtection", question);
treeNode.put("passwordProtectionAnswer", answer);
treeNode.put("paymentPassword", MD5Util.md5(password));
// url
String url = URLs.ACCOUNT_SET_PAYMENTPASSWORD;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return setPaymentPassword(appContext, idCard, question, answer,
password);
} else {
throw (AppException) e;
}
}
}
/**
* 修改支付密码
*
* @param appContext
* @param oldPsd
* @param newPsd
* @return
*/
public static int resetPaymentPassword(AppContext appContext,
String oldPsd, String newPsd) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("oldPaymentPassword", MD5Util.md5(oldPsd));
treeNode.put("newPaymentPassword", MD5Util.md5(newPsd));
// url
String url = URLs.ACCOUNT_RESET_PAYMENTPASSWORD;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return resetPaymentPassword(appContext, oldPsd, newPsd);
} else {
throw (AppException) e;
}
}
}
/**
* 获取密码保护问题
*
* @param appContext
* @return
* @throws AppException
*/
public static String getPasswordProtection(AppContext appContext)
throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
// url
String url = URLs.ACCOUNT_GET_PAYMENTQUSTION;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
JsonNode dataNode = rootNode.path("details");
JsonNode dNode = om.readTree(dataNode.toString());
String sResult = dNode.path("passwordProtection")
.getValueAsText();
return sResult;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return getPasswordProtection(appContext);
} else {
throw (AppException) e;
}
}
}
/**
* 提现
*
* @param account
* @param money
* @param password
* @return
*/
public static int checkout(AppContext appContext, String account,
float money, String password) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("bank", account);
treeNode.put("to", 1);
treeNode.put("money", money);
treeNode.put("password", MD5Util.md5(password));
// url
String url = URLs.ACCOUNT_CHECHOUT;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return checkout(appContext, account, money, password);
} else {
throw (AppException) e;
}
}
}
/**
* 认证
*
* @param appContext
* @param authType
* 0:个人认证 ;1:企业认证
* @param authName
* @param authHead
* @return
* @throws AppException
*/
public static int setAuth(AppContext appContext, int authType,
String authName, String authHead) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("authType", authType);
treeNode.put("authName", authName);
treeNode.put("authHead", authHead);
// url
String url = URLs.ACCOUNT_AUTH;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return setAuth(appContext, authType, authName, authHead);
} else {
throw (AppException) e;
}
}
}
/**
* 社交账号绑定
*
* @param appContext
* @param uid
* @param token
* @param expiresTime
* @param type
* @return
*/
public static int bind(AppContext appContext, String uid, String token,
long expiresTime, int type) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("userId", appContext.getLoginUid());
treeNode.put("uid", uid);
treeNode.put("token", token);
treeNode.put("expiresTime", expiresTime);
treeNode.put("platType", type);
// url
String url = URLs.USER_BIND;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
return 1;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return bind(appContext, uid, token, expiresTime, type);
} else {
throw (AppException) e;
}
}
}
/**
* 快捷登陆
*
* @param appContext
* @param account
* @param uid
* @param userName
* @param head
* @param token
* @param expiresTime
* @return
*/
public static BLogin easyLogin(AppContext appContext, int loginType,
String account, String uid, String userName, String head,
String token, long expiresTime) throws AppException {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode treeNode = objectMapper.createObjectNode();
treeNode.put("loginType", loginType);
treeNode.put("account", account);
treeNode.put("userHeadUrl", head);
treeNode.put("userName", userName);
treeNode.put("uid", uid);
treeNode.put("token", token);
treeNode.put("expiresTime", expiresTime);
// url
String url = URLs.EASY_LOGIN;
// 请求json
String requestJson = getRequestJson(appContext, treeNode);
try {
InputStream inputStream = _post(url, requestJson);
ObjectMapper om = new ObjectMapper();
JsonNode rootNode = om.readTree(inputStream);
int result = rootNode.path("result").getIntValue();
if (result == 0) {
// 失败
String why = rootNode.path("description").getTextValue();
throw AppException.fail(why);
} else if (result == 1) {
JsonNode dataNode = rootNode.path("details");
JsonNode dNode = om.readTree(dataNode.toString());
int userId = dNode.path("userId").getValueAsInt();
String userHeadUrl = dNode.path("userHeadUrl").getValueAsText();
String name = dNode.path("userName").getValueAsText();
String psw = dNode.path("password").getValueAsText();
BLogin bLogin = new BLogin();
bLogin.setName(name);
bLogin.setUid(userId);
bLogin.setFace(userHeadUrl);
bLogin.setPwd(psw);
return bLogin;
} else {
throw AppException.fail("接口异常");
}
} catch (JsonProcessingException e) {
L.i("json转换失败");
throw AppException.json(e);
} catch (IOException e) {
L.i("数据读取异常");
throw AppException.io(e);
} catch (AppException e) {
if (filterException(appContext, e)) {
return easyLogin(appContext, loginType, account, uid, userName,
head, token, expiresTime);
} else {
throw (AppException) e;
}
}
}
}