public class SingleHttpClient {
private static DefaultHttpClient httpClient = new DefaultHttpClient();
private static SingleHttpClient shc = new SingleHttpClient();
private static CookieStore cookieStore; //cookie本地cookie
public static SingleHttpClient getInstance() {
if (cookieStore != null) {
httpClient.setCookieStore(cookieStore);
}
return shc;
}
private SingleHttpClient() {
BasicHttpParams httpParams = (BasicHttpParams) httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
}
/**
*
* @param url
* @return
*/
public synchronized String httpGet(String param, Context context) {
synchronized (httpClient) {
if (!PublicUtil.isNetworkAvailable(context))
return null;
String url = PublicUtil.getWebRoot() + param; // 我的url
String response = null;
// 鍒涘缓HttpGet瀵硅薄
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse;
try {
// 浣跨敤execute鏂规硶鍙戦?HTTP GET璇锋眰锛屽苟杩斿洖HttpResponse瀵硅薄
long start = new Date().getTime();
httpResponse = httpClient.execute(httpGet);
long end = new Date().getTime();
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
response = EntityUtils.toString(httpResponse.getEntity(),
"UTF-8");
} else {
PublicUtil.sendMessage(context, PublicUtil.NETWORK_BUSY); // 网络请求超时
}
cookieStore = httpClient.getCookieStore();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("response=" + response);
return response;
}
}
public HashMap<String, String> getResponse(Context context, String param) {
HashMap<String, String> info = null;
String response = httpGet(param, context);
System.out.println("response==" + response);
if (response != null) {
info = new HashMap<String, String>();
try {
JSONObject jobj = new JSONObject(response);
String code = jobj.getString("code");
info.put("code", code);
String msg = jobj.getString("msg");
info.put("msg", msg);
if (jobj.getString("data") != null) {
String data = jobj.getString("data");
info.put("data", data);
}
if (code.equals("-2")) {
MultipartEntity mpEntity = new MultipartEntity();
String account = "用户名";
String loginKey = "密码";
try {
mpEntity.addPart("account", new StringBody(account));
mpEntity.addPart("loginKey", new StringBody(
loginKey));
Log.i("tcp", "loginKey=" + loginKey);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> resp = getResponse(context, mpEntity, "/mobile/login");
if(resp!=null&&resp.get("code").equals("0"))
info = getResponse(context, param);
}
}
} catch (Exception e) {
}
}
return info;
}
public HashMap<String, String> getResponse(Context context,
MultipartEntity entity, String url) {
HashMap<String, String> info = null;
String response = httpPost(url, entity, context);
System.out.println("response==" + response);
if (response != null) {
info = new HashMap<String, String>();
String code = null;
try {
JSONObject jobj = new JSONObject(response);
code = jobj.getString("code");
info.put("code", code);
String msg = jobj.getString("msg");
info.put("msg", msg);
if (jobj.has("data")) {
String data = jobj.getString("data");
info.put("data", data);
}
if(code.equals("-1")){
}else if (code.equals("-2")) {
MultipartEntity mpEntity = new MultipartEntity();
String account = "用户名"
if (!account.equals("")
&& PublicUtil.isNetworkAvailable(context)) {
String loginKey = "密码";
try {
mpEntity.addPart("account", new StringBody(account));
mpEntity.addPart("loginKey", new StringBody(
loginKey));
Log.i("tcp", "loginKey=" + loginKey);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashMap<String, String> resp = getResponse(context, mpEntity, "/mobile/login");
if(resp!=null&&resp.get("code").equals("0"))
info = getResponse(context, mpEntity, url);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return info;
}
/**
*
*
* @param url
* @param params
*
* @return
* @throws Exception
*/
public String httpPost(String url, MultipartEntity entity, Context context) {
synchronized (httpClient) {
if (!PublicUtil.isNetworkAvailable(context))
return null;
String response = null;
HttpPost httppost = new HttpPost(PublicVariable.webRoot + url);
HashMap<String, String> map = (HashMap<String, String>) PublicUtil
.getCallInfo(context).clone();
map.put("path", url.split("\\?")[0]);
map.put("reqSize", url.getBytes().length + "");
Log.i("post", "post:"+ PublicVariable.webRoot+url);
try {
// httppost.setEntity(new UrlEncodedFormEntity(params,
// HTTP.UTF_8));
httppost.setEntity(entity);
long start = new Date().getTime();
// 浣跨敤execute鏂规硶鍙戯拷?HTTP Post璇锋眰锛屽苟杩斿洖HttpResponse瀵硅薄
HttpResponse httpResponse = httpClient.execute(httppost);
long end = new Date().getTime();
map.put("totalConsumed", (end - start) + "");
int statusCode = httpResponse.getStatusLine().getStatusCode();
map.put("respStatus", statusCode + "");
response = EntityUtils.toString(httpResponse.getEntity(),
"UTF-8");
Log.i("tcp",
"statusCode=" + statusCode + ""
+ response.replace("\n", ""));
if (statusCode == HttpStatus.SC_OK) {
} else {
response = null;
if(statusCode==500)
PublicUtil.sendMessage(context, PublicUtil.NETWORK_BUSY);
}
cookieStore = httpClient.getCookieStore();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}