封装

package com.cytech.datingtreasure.http;


import io.rong.imkit.RongIM;
import io.rong.imkit.utils.HTMLDecoder;
import io.rong.imkit.utils.HighLightUtils;
import io.rong.imkit.utils.Util;
import io.rong.imlib.RongIMClient.Conversation;
import io.rong.imlib.RongIMClient.ConversationType;
import io.rong.message.ImageMessage;
import io.rong.message.LocationMessage;
import io.rong.message.RichContentMessage;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;


import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.http.NameValuePair;
import org.json.JSONObject;


import com.cytech.datingtreasure.app.db.model.BaseModel;
import com.cytech.datingtreasure.app.db.model.CheckAppVersionModel;
import com.cytech.datingtreasure.app.db.model.ChecknicknameModel;
import com.cytech.datingtreasure.app.db.model.GetActivityListModel;
import com.cytech.datingtreasure.app.db.model.GetChatMsgListModel;
import com.cytech.datingtreasure.app.db.model.GetCityListModel;
import com.cytech.datingtreasure.app.db.model.GetCoinModel;
import com.cytech.datingtreasure.app.db.model.GetCommentListModel;
import com.cytech.datingtreasure.app.db.model.GetCommentModel;
import com.cytech.datingtreasure.app.db.model.GetContactListModel;
import com.cytech.datingtreasure.app.db.model.GetDateDetailModel;
import com.cytech.datingtreasure.app.db.model.GetDateListModel;
import com.cytech.datingtreasure.app.db.model.GetDictModel;
import com.cytech.datingtreasure.app.db.model.GetMobileModel;
import com.cytech.datingtreasure.app.db.model.GetMyCoinModel;
import com.cytech.datingtreasure.app.db.model.GetMyselfModel;
import com.cytech.datingtreasure.app.db.model.GetNearPersonListModel;
import com.cytech.datingtreasure.app.db.model.GetQiniuTokenModel;
import com.cytech.datingtreasure.app.db.model.GetRankModel;
import com.cytech.datingtreasure.app.db.model.GetThemeListModel;
import com.cytech.datingtreasure.app.db.model.GetWxAccessTokenModel;
import com.cytech.datingtreasure.app.db.model.PublicModel;
import com.cytech.datingtreasure.app.db.model.UploadFileModel;
import com.cytech.datingtreasure.app.db.model.UploadModel;
import com.cytech.datingtreasure.app.db.model.detail.LDCustomeMessage;
import com.cytech.datingtreasure.app.db.model.detail.MsgModel;
import com.cytech.datingtreasure.app.db.model.detail.OrderModel;
import com.cytech.datingtreasure.helper.BitmapUtil;
import com.cytech.datingtreasure.helper.ConfigUtil;
import com.cytech.datingtreasure.helper.FileUtil;


import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.util.Log;




/**
 * 异步执行UI线程
 * 
 * @author ld 
 * 20140505
 * 
 */
public class UIAsnTask extends BaseHandlerUI implements Runnable {
private Handler mHandler = new Handler();
private int request_code;
private String result;
private Context context;


private List params = new ArrayList();
private Map map = new HashMap();


private void initParams(Handler mHandler, int request_code) {
this.mHandler = mHandler;
this.request_code = request_code;
}


/**
* get请求构造函数

* @param mHandler
* @param map
* @param request_code
*/
public UIAsnTask(Context context,Handler mHandler, Map map, int request_code) {
super();
initParams(mHandler, request_code);
this.map = map;
this.context = context;
}
private boolean show_all = true;
/**
* get请求构造函数

* @param mHandler
* @param map
* @param request_code
*/
public UIAsnTask(Context context,Handler mHandler, Map map, int request_code,boolean show_all) {
super();
initParams(mHandler, request_code);
this.map = map;
this.context = context;
this.show_all = show_all;
}
/**
* get请求构造函数

* @param mHandler
* @param map
* @param request_code
*/
public UIAsnTask(Handler mHandler, String city_name, int request_code,Activity context) {
super();
initParams(mHandler, request_code);
this.context = context;
}


/**
* post请求构造函数

* @param mHandler
* @param map
* @param request_code
*/
public UIAsnTask(Handler mHandler, List params,
int request_code) {
super();
initParams(mHandler, request_code);
this.params = params;
}
/**
* post请求构造函数

* @param mHandler
* @param map
* @param request_code
*/
public UIAsnTask(Context context,Handler mHandler,
int request_code) {
super();
initParams(mHandler, request_code);
this.context = context;
}
/**
* post请求构造函数

* @param mHandler
* @param map
* @param request_code
*/
public UIAsnTask(Handler mHandler,
int request_code) {
super();
initParams(mHandler, request_code);
}
public List paths;

public int uin;


@Override
public void run() {
// TODO Auto-generated method stub
excute();


}


/**
* 线程执行
*/
private void excute() {
switch (request_code) {
case get_wx_access_token_code://通过code获取access_token
result = HttpUtils.doPost("https://api.weixin.qq.com/sns/oauth2/access_token", params);
GetWxAccessTokenModel mGetWxAccessTokenModel = JsonUtils.getWxAccessToken(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetWxAccessTokenModel));
break;
}
break;
case GetCityList_code://获取城市列表
result = FileUtil.readFromAsset(context, "data.txt");
GetCityListModel mGetCityListModel = JsonUtils.getCityList(show_all,result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetCityListModel));
}
break;
case base_request_code://基本请求
result = HttpUtils.doPost(Urls.url_pre, params);
BaseModel mBaseModel = JsonUtils.getResult(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mBaseModel));
break;
}
case GetSmsCodeService_getRegSmsCode_code://获取验证码(忘记密码,手机)
result = HttpUtils.doPost(Urls.url_pre, params);
mBaseModel = JsonUtils.getResult(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mBaseModel));
break;
}
case UserInfoService_attMobile_code://绑定手机号 
result = HttpUtils.doPost(Urls.url_pre, params);
mBaseModel = JsonUtils.getResult(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mBaseModel));
break;
}
case UserInfoService_unAttMobile_code://解除绑定手机号 
result = HttpUtils.doPost(Urls.url_pre, params);
mBaseModel = JsonUtils.getResult(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mBaseModel));
break;
}
case GetSmsCodeService_getAttachedSmsCode_code://获取验证码(绑定手机号)
result = HttpUtils.doPost(Urls.url_pre, params);
mBaseModel = JsonUtils.getResult(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mBaseModel));
break;
case uploadFile_code://上传文件
result = HttpUtils.doPostFile(Urls.api_uploadFile,getCompressImgs(paths));
UploadFileModel mUploadFileModel = JsonUtils.uploadFile(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mUploadFileModel));
break;
}
case UserInfoService_login_code://登录
result = HttpUtils.doPost(Urls.url_pre, params);
mBaseModel = JsonUtils.getResult(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mBaseModel));
break;
case UploadPicService_getQiniuToken_code://七牛token
result = HttpUtils.doPost(Urls.url_pre, params);
GetQiniuTokenModel mGetQiniuTokenModel = JsonUtils.getQiniuToken(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetQiniuTokenModel));
break;
}
case UserAccountService_myCoin_code://我的账户
result = HttpUtils.doPost(Urls.url_pre, params);
GetMyCoinModel mGetMyCoinModel = JsonUtils.getMyCoins(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetMyCoinModel));
break;
}
case GetCoinService_coin_code://获取金币(成功分享,或者用户在登录成功后)
result = HttpUtils.doPost(Urls.url_pre, params);
GetCoinModel mGetCoinModel = JsonUtils.coin(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetCoinModel));
break;
}
case ActivityService_list_code://活动列表
result = HttpUtils.doPost(Urls.url_pre, params);
GetActivityListModel mGetActivityListModel = JsonUtils.getActivityList(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetActivityListModel));
break;
}
case UserPayService_pay_code://付费查看手机号码 
result = HttpUtils.doPost(Urls.url_pre, params);
GetMobileModel mGetMobileModel = JsonUtils.getMobiel(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetMobileModel));
}
break;
case UserInfoService_getThemeList_code://查看主题列表
result = HttpUtils.doPost(Urls.url_pre, params);
GetThemeListModel mGetThemeListModel = JsonUtils.getThemeList(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetThemeListModel));
}
break;
case SystemMsgService_getDict_code://系统配置
result = HttpUtils.doPost(Urls.url_pre, params);
GetDictModel mGetDictModel = JsonUtils.getDict(result);
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetDictModel));
}
break;
case get_chat_msg_code:// 获取消息列表
GetChatMsgListModel mGetChatMsgListModel = getMessageData();
if (mHandler != null) {
mHandler.sendMessage(mHandler.obtainMessage(
TASK_NOTIFY_RETURN_DATA, mGetChatMsgListModel));
break;
}
}
}
public List list;
/**
* 获取聊天消息
* @return
*/
private GetChatMsgListModel getMessageData(){
list = RongIM.getInstance().getConversationList();
GetChatMsgListModel mGetChatMsgListModel = new GetChatMsgListModel();
mGetChatMsgListModel.conversation_list = list;
List models = new ArrayList();
for (int i = 0; i < list.size(); i++) {
if(list.get(i).getLatestMessage() instanceof TextMessage){
TextMessage textMessage = (TextMessage) list.get(i).getLatestMessage();
String str = HTMLDecoder.decode(HTMLDecoder.decode(textMessage.getContent()));
SpannableStringBuilder txt_content = Util.highLight(HighLightUtils.loadHighLight(str));
// SpannableStringBuilder txt_content = new SpannableStringBuilder(str);
MsgModel m = new MsgModel(list.get(i).getTargetId(),list.get(i).getSenderUserName(), txt_content, 
"", RongIM.getInstance().getUnreadCount(ConversationType.PRIVATE,list.get(i).getTargetId()),
list.get(i).getReceivedTime());
models.add(m);
}else if(list.get(i).getLatestMessage() instanceof VoiceMessage){
SpannableStringBuilder ss = new SpannableStringBuilder("[语音]");
models.add(new MsgModel(list.get(i).getTargetId(),list.get(i).getSenderUserName(), ss, 
"", RongIM.getInstance().getUnreadCount(ConversationType.PRIVATE,list.get(i).getTargetId()),list.get(i).getReceivedTime()));
}else if(list.get(i).getLatestMessage() instanceof LocationMessage){
SpannableStringBuilder ss = new SpannableStringBuilder("[位置]");
models.add(new MsgModel(list.get(i).getTargetId(),list.get(i).getSenderUserName(), ss, 
"", RongIM.getInstance().getUnreadCount(ConversationType.PRIVATE,list.get(i).getTargetId()),list.get(i).getReceivedTime()));
}else if(list.get(i).getLatestMessage() instanceof ImageMessage){
SpannableStringBuilder ss = new SpannableStringBuilder("[图片]");
models.add(new MsgModel(list.get(i).getTargetId(),list.get(i).getSenderUserName(), ss, 
"", RongIM.getInstance().getUnreadCount(ConversationType.PRIVATE,list.get(i).getTargetId()),list.get(i).getReceivedTime()));
}else if(list.get(i).getLatestMessage() instanceof RichContentMessage){
RichContentMessage mRichContentMessage = (RichContentMessage) list.get(i).getLatestMessage();
SpannableStringBuilder ss = new SpannableStringBuilder(mRichContentMessage.getTitle());
models.add(new MsgModel(list.get(i).getTargetId(),list.get(i).getSenderUserName(), ss, 
"", RongIM.getInstance().getUnreadCount(ConversationType.PRIVATE,list.get(i).getTargetId()),list.get(i).getReceivedTime()));
}
}
mGetChatMsgListModel.msg_list = models;
return mGetChatMsgListModel;
}










private List getCompressImgs(List selectedDataList){
List files = new ArrayList();
if(!ConfigUtil.isEmpty(selectedDataList)){
for (int i = 0; i < selectedDataList.size(); i++) {
String toPath = BitmapUtil.getCompressImagePath(selectedDataList.get(i), FileUtil.getTempDir(), "/temp_" + i + ".jpg");
files.add(new File(toPath));
}
}
return files;
}

}



package com.cytech.datingtreasure.http;




import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


import org.apache.commons.httpclient.methods.PostMethod;
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.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;


import com.cytech.datingtreasure.Config;
import com.cytech.datingtreasure.helper.MD5;
import com.cytech.datingtreasure.http.upload.FormFile;
import com.cytech.datingtreasure.http.upload.MyHttpsClient;
import com.cytech.datingtreasure.http.upload.SocketHttpRequester;


import android.util.Log;






/**
 * http请求工具类
 * @author ld
 * 20130715
 *
 */
public class HttpUtils {
private static String encode_charect = "UTF8";
private static String FIX_VERIFY = "yuudhj4dsk33jwiex2u";//固定验证码
private static int HTTP_TIMEOUT = 60;//http连接超时时间




/**
* 获取带加密参数的url
* @param url
* @return
*/
private static String getVerifyUrl(String url){
return getVerifyUrl(url, false);
}




/**
* 获取带加密参数的url
* @param url
* @return
*/
private static String getVerifyUrl(String url, boolean use_fix_verify){
url = url + "&stime=" + System.currentTimeMillis();
if (!url.startsWith("sid=") && !url.contains("&sid=") && !url.contains("?sid=")) {
//参数中不含用户id的,则加入用户id
// url = url + "&sid=" + SHApp.user.id;
}
String encryptUrl = url;
if (url.indexOf("?") >= 0)  {
encryptUrl = url.substring(url.indexOf("?")+1);
}
String Prestr = encryptUrl;
if (!use_fix_verify) {
/*if (null != SHApp.verify_code) {
Prestr = SHApp.verify_code + encryptUrl  + SHApp.verify_code;
}*/
} else {
Prestr = FIX_VERIFY + encryptUrl  + FIX_VERIFY;
}
MD5 md5 = new MD5();
//ConfigUtils.debug("MD5:" + Prestr);
String Sign = md5.getMD5ofStr(Prestr);
url = url + "&sig=" + Sign;
return url;
}


/*public static String doPost(String url, List params)  {
Map map = new HashMap();
return doPost(url, map, params, false);
}*/




public static String doPost(String url, List params)  {
if(Config.debug){
Log.i("DatingPost",url);
Log.i("DatingPost",params.toString());
}
HttpPost httpRequest = new HttpPost(url);  
String strResult = "";  
try {  
/* 添加请求参数到请求对象 */  
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));  
/* 发送请求并等待响应 */  
HttpResponse httpResponse = getHttpClient().execute(httpRequest);  
/* 若状态码为200 ok */  
if (httpResponse.getStatusLine().getStatusCode() == 200) {  
/* 读返回数据 */  
strResult = EntityUtils.toString(httpResponse.getEntity(),"gbk");  
}  else {
if(Config.debug){
Log.i("DatingPost",httpResponse.getStatusLine().getStatusCode() + "");
}
}
} catch (ClientProtocolException e) {  
e.printStackTrace();  
} catch (IOException e) {  
e.printStackTrace();  
} catch (Exception e) {  
e.printStackTrace();  
}  
if(Config.debug){
Log.i("DatingPost",strResult);
}
return strResult;  
}  


/**
* 创建httpclient对象
* @return
*/
public static HttpClient getHttpClient() { 


// 创建 HttpParams 以用来设置 HTTP 参数(这一部分不是必需的)  


HttpParams httpParams = new BasicHttpParams();  


// 设置连接超时和 Socket 超时,以及 Socket 缓存大小  


HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_TIMEOUT * 1000);  


HttpConnectionParams.setSoTimeout(httpParams, HTTP_TIMEOUT * 1000);  


HttpConnectionParams.setSocketBufferSize(httpParams, 8192);  


// 设置重定向,缺省为 true  


HttpClientParams.setRedirecting(httpParams, true);  


// 设置 user agent  


String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";  
HttpProtocolParams.setUserAgent(httpParams, userAgent);  


// 创建一个 HttpClient 实例  


// 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient  


// 中的用法,在 Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient  


DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);  


enableSSL(httpClient);


return httpClient;  
}  


/** 
* 访问https的网站 
* @param httpclient 
*/  
private static void enableSSL(DefaultHttpClient httpclient){  
//调用ssl  
try {  
SSLContext sslcontext = SSLContext.getInstance("TLS");  
sslcontext.init(null, new TrustManager[] { truseAllManager }, null);  
SSLSocketFactory sf = new SSLSocketFactory(null);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
Scheme https = new Scheme("https", sf, 443);  
httpclient.getConnectionManager().getSchemeRegistry().register(https);  
} catch (Exception e) {  
e.printStackTrace();  
}  
}  


/** 
* 重写验证方法,取消检测ssl 
*/  
private static TrustManager truseAllManager = new X509TrustManager(){  
public void checkClientTrusted(  
java.security.cert.X509Certificate[] arg0, String arg1)  
throws CertificateException {  
// TODO Auto-generated method stub  
}  
public void checkServerTrusted(  
java.security.cert.X509Certificate[] arg0, String arg1)  
throws CertificateException {  
// TODO Auto-generated method stub  
}  
public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
// TODO Auto-generated method stub  
return null;  
}  
}; 


/**
* http get请求
* @param url
* @param params
* @return
* @throws UnsupportedEncodingException 
*/
@SuppressWarnings("rawtypes")
public static String doGet(String url, Map map) {
return doGet(url, map);
}


/**
* http get请求
* @param url
* @param params
* @return
* @throws UnsupportedEncodingException 
*/
@SuppressWarnings("rawtypes")
public static String doGet(String url) {  
String strResult = "doGetError";  
try {  
HttpGet httpRequest = new HttpGet(url);  
HttpResponse httpResponse = getHttpClient().execute(httpRequest);  
if (httpResponse.getStatusLine().getStatusCode() == 200) {  
strResult = EntityUtils.toString(httpResponse.getEntity(),"gbk");  
} else {
}
} catch (ClientProtocolException e) {  
e.printStackTrace();  
} catch (IOException e) {  
e.printStackTrace();  
} catch (Exception e) {  
e.printStackTrace();  
}  
return strResult;  
}  




/**
* 上传图片到服务器

* @param imageFile 包含路径
*/
public static void uploadFile(File imageFile) {
try {
String actionUrl = Urls.api_uploadFile;
//请求普通信息
Map files = new HashMap();
files.put("file", imageFile);
//上传文件
FormFile formfile = new FormFile(imageFile.getName(), imageFile, "file", "Content-Type: image/jpeg");
SocketHttpRequester.post(actionUrl, null,formfile);
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* post文件
* @param url
* @param map
* @param paths
* @return
*/
public static String doPostFile(String url,List files) {


int status=0; 
String strResult = "";
try {
PostMethod postMethod = upFileMethod(url,files);
status = MyHttpsClient.getMyHttpsClient().executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
strResult = postMethod.getResponseBodyAsString();
} else {
}
} catch (Exception e) {
}


return checkFormat(strResult);  
}
private static PostMethod upFileMethod(String url,List files) throws FileNotFoundException {
PostMethod filePost = new PostMethod(url);
filePost.getParams().setContentCharset("UTF-8");


int len = files.size();
Part[] parts = new Part[len];
for (int i = 0; i < len; i++) {
File targetFile = files.get(i);
FilePart fp = new FilePart("file", targetFile);
parts[i] = fp;
fp.setContentType("Content-Type: image/jpeg");
}
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost
.getParams()));
return filePost;
}
/**
* 检查字符串是否为json字符串
* @param result
* @return
*/
private static String checkFormat(String result){
try {
@SuppressWarnings("unused")
JSONObject jsonObject = new JSONObject(result);
}catch (Exception e) {
}
if(Config.debug){
Log.i("LovePostFile",result);
}
return result;
}


}





package com.cytech.datingtreasure.http;


import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.cytech.datingtreasure.app.db.model.BaseModel;
import com.cytech.datingtreasure.app.db.model.CheckAppVersionModel;
import com.cytech.datingtreasure.app.db.model.ChecknicknameModel;
import com.cytech.datingtreasure.app.db.model.GetActivityListModel;
import com.cytech.datingtreasure.app.db.model.GetCityListModel;
import com.cytech.datingtreasure.app.db.model.GetCoinModel;
import com.cytech.datingtreasure.app.db.model.GetCommentListModel;
import com.cytech.datingtreasure.app.db.model.GetCommentModel;
import com.cytech.datingtreasure.app.db.model.GetContactListModel;
import com.cytech.datingtreasure.app.db.model.GetDateDetailModel;
import com.cytech.datingtreasure.app.db.model.GetDateListModel;
import com.cytech.datingtreasure.app.db.model.GetDictModel;
import com.cytech.datingtreasure.app.db.model.GetMobileModel;
import com.cytech.datingtreasure.app.db.model.GetMyCoinModel;
import com.cytech.datingtreasure.app.db.model.GetMyselfModel;
import com.cytech.datingtreasure.app.db.model.GetNearPersonListModel;
import com.cytech.datingtreasure.app.db.model.GetQiniuTokenModel;
import com.cytech.datingtreasure.app.db.model.GetRankModel;
import com.cytech.datingtreasure.app.db.model.GetThemeListModel;
import com.cytech.datingtreasure.app.db.model.GetWxAccessTokenModel;
import com.cytech.datingtreasure.app.db.model.PublicModel;
import com.cytech.datingtreasure.app.db.model.UploadFileModel;
import com.cytech.datingtreasure.app.db.model.UploadModel;
import com.cytech.datingtreasure.app.db.model.detail.ActivityListModel;
import com.cytech.datingtreasure.app.db.model.detail.CityModel;
import com.cytech.datingtreasure.app.db.model.detail.CommentModel;
import com.cytech.datingtreasure.app.db.model.detail.DateInfoModel;
import com.cytech.datingtreasure.app.db.model.detail.DateModel;
import com.cytech.datingtreasure.app.db.model.detail.InterestModel;
import com.cytech.datingtreasure.app.db.model.detail.OrderModel;
import com.cytech.datingtreasure.app.db.model.detail.PhotoModel;
import com.cytech.datingtreasure.app.db.model.detail.QuestionModel;
import com.cytech.datingtreasure.app.db.model.detail.SoundInfoModel;
import com.cytech.datingtreasure.app.db.model.detail.ThemeModel;
import com.cytech.datingtreasure.app.db.model.detail.UploadPathModel;
import com.cytech.datingtreasure.app.db.model.detail.UserInfoModel;
import com.cytech.datingtreasure.helper.ConfigUtil;
import com.google.gson.JsonObject;



public class JsonUtils {
/**
* 解析基本结果集
* @param result
* @return
*/
public static BaseModel getResult(String result){
BaseModel bean = new BaseModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("coin")){
bean.coin = jsonObject.getInt("coin");
}
if(jsonObject.has("data")){
if(jsonObject.has("server_time")){
bean.server_time = jsonObject.getLong("server_time");
}
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("token")){
bean.token = json_data.getString("token");
}
if(json_data.has("coin")){
bean.coin = json_data.getInt("coin");
}
}
}catch (Exception e) {
}
return bean;
}
/**
* 系统配置
* @param result
* @return
*/
public static GetDictModel getDict(String result){
GetDictModel bean = new GetDictModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("coin")){
bean.coin = jsonObject.getInt("coin");
}
if(jsonObject.has("data")){
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("coin")){
bean.coin = json_data.getInt("coin");
}
if(json_data.has("view_mobile_vip")){
bean.view_mobile_vip = json_data.getInt("view_mobile_vip");
}
if(json_data.has("view_mobile_not_vip")){
bean.view_mobile_not_vip = json_data.getInt("view_mobile_not_vip");
}
}
}catch (Exception e) {
}
return bean;
}
/**
* 付费查看手机号码 
* @param result
* @return
*/
public static GetMobileModel getMobiel(String result){
GetMobileModel bean = new GetMobileModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("coin")){
bean.coin = jsonObject.getInt("coin");
}
if(jsonObject.has("data")){
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("mobile")){
bean.mobile = json_data.getString("mobile");
}
}
}catch (Exception e) {
}
return bean;
}

/**
* 解析基本结果集
* @param result
* @return
*/
public static GetWxAccessTokenModel getWxAccessToken(String result){
GetWxAccessTokenModel bean = new GetWxAccessTokenModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
if(jsonObject.has("errcode")){
bean.retcode = jsonObject.getInt("errcode");
}
if(jsonObject.has("errmsg")){
bean.msg = jsonObject.getString("errmsg");
}
if(jsonObject.has("access_token")){
bean.access_token = jsonObject.getString("access_token");
}
if(jsonObject.has("expires_in")){
bean.expires_in = jsonObject.getString("expires_in");
}
if(jsonObject.has("refresh_token")){
bean.refresh_token = jsonObject.getString("refresh_token");
}
if(jsonObject.has("openid")){
bean.openid = jsonObject.getString("openid");
}
if(jsonObject.has("scope")){
bean.scope = jsonObject.getString("scope");
}
}catch (Exception e) {
}
return bean;
}

/**
* 解析基本结果集
* @param result
* @return
*/
public static PublicModel publicDate(String result){
PublicModel bean = new PublicModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("data")){
if(jsonObject.has("server_time")){
bean.server_time = jsonObject.getLong("server_time");
}
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("d_id")){
bean.d_id = json_data.getInt("d_id");
}
if(json_data.has("coin")){
bean.coin = json_data.getInt("coin");
}
}
}catch (Exception e) {
}
return bean;
}

/**
* 评论
* @param result
* @return
*/
public static GetCommentModel getComment(String result){
GetCommentModel bean = new GetCommentModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("data")){
if(jsonObject.has("server_time")){
bean.server_time = jsonObject.getLong("server_time");
}
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("coin")){
bean.coin = json_data.getInt("coin");
}
}
}catch (Exception e) {
}
return bean;
}

/**
* 下单
* @param result
* @return
*/
public static OrderModel order(String result){
OrderModel bean = new OrderModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("data")){
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("order_id")){
bean.order_id = json_data.getString("order_id");
}
if(json_data.has("price")){
bean.price = json_data.getDouble("price");
}
}
}catch (Exception e) {
}
return bean;
}

/**
* 增加照片
* @param result
* @return
*/
public static UploadModel upload(String result){
UploadModel bean = new UploadModel();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
bean.retcode = jsonObject.getInt("retcode");
if(jsonObject.has("msg")){
bean.msg = jsonObject.getString("msg");
}
if(jsonObject.has("data")){
if(jsonObject.has("server_time")){
bean.server_time = jsonObject.getLong("server_time");
}
JSONObject json_data = jsonObject.getJSONObject("data");
if(json_data.has("ids")){
List upload_list = new ArrayList();
JSONArray id_array = json_data.getJSONArray("ids");
for (int j = 0; j < id_array.length(); j++) {
int id =  (Integer) id_array.opt(j);
upload_list.add(id);
}
bean.ids = upload_list;
}
}
}catch (Exception e) {
}
return bean;
}







package com.cytech.datingtreasure.http;


import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;


import android.os.Handler;








/**
 * 线程管理
 * @author ld
 * 20130929
 *
 */
public class Controller {
private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 15;
private static final int KEEP_ALIVE = 5;
private static final BlockingQueue sWorkQueue = new LinkedBlockingQueue(
MAXIMUM_POOL_SIZE);


private static final ThreadFactory sThreadFactory = new ThreadFactory() {
private AtomicInteger mCount = new AtomicInteger(1);


public Thread newThread(Runnable r) {
int count = mCount.getAndIncrement();
return new Thread(r, count + "_thread");
}
};
private static RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy();


private Handler _controlHandler = null;
public Handler getControlHandler() {
if (_controlHandler != null) {
return _controlHandler;
}
return null;
}
private static final ThreadPoolExecutor sExecutor = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS,
sWorkQueue, sThreadFactory, rejectedExecutionHandler);
private static Controller _controller = null;


public static Controller getInstance() {
if (_controller == null) {
_controller = new Controller();
}
return _controller;
}
/**
* 执行一个 RUN 任务

* @param httpThreadTask
*/
public synchronized void execute(UIAsnTask task) {
sExecutor.execute(task);
}
}

你可能感兴趣的:(android)