}
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
Map map = new HashMap();
return doPost(url, map, params, false);
}*/
public static String doPost(String url, List
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.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
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
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
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
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);
}
}