最终调用时的代码
private void ansyClearApplyInfor() {
RequestParams params = new RequestParams();params.put("uid", AppUser.getInstance().getUser().getuId());params.put("session_id", AppUser.getInstance().getUser().getuSessionId());params.put("family_id", AppUser.getInstance().getUser().getUserFamily().getId());//申请加入的帮会idDialogUtil.showProgressDialog(this, "", "请求中...", true, null);
AsyncXiuHttpHelper.post(Constants.GROUP_APPLY_CLEAN, params, new OnHttpListener<JSONObject>() {@Overridepublic void onHttpListener(boolean httpSuccessed, JSONObject obj) {DialogUtil.dismissProgressDialog();
Log.i("-bqt", "=====点击清空申请记录后返回的数据:" + obj.toString());if (httpSuccessed) {if (obj.optInt("result", -1) == 1) {} else {ApplicationUtil.showToast(FamilyApplyManageActivity.this, obj.optString("msg", "失败"));}} else {ApplicationUtil.showToast(FamilyApplyManageActivity.this, "网络错误");}}});}
公司封装的http请求帮助类
public class AsyncXiuHttpHelper {public static final String SERVER_URL = "api.95xiu.com";public static final String WEB_SERVER_URL = "chat.95xiu.com";public static final int LIVE_WEB_PORT = 3016;public static String getAbsoluteUrl(String relativeUrl) {return AsyncHttpHelper.getAbsoluteUrl(SERVER_URL, relativeUrl);}/*** 返回基础RequestParams*/private static RequestParams formatRequestParams(RequestParams params) {if (params == null) params = new RequestParams();params.put("imei", Properties.IMEI);params.put("channel", Properties.CN);params.put("session_id", AppUser.getInstance().getUser().getuSessionId());params.put("version_code", Properties.VERSION_CODE);return params;}/*** get 请求*/public static void get(final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {params = formatRequestParams(params);AsyncHttpHelper.get(SERVER_URL, relativeUrl, params, onHttpListner);}/*** post 请求*/public static void post(final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {params = formatRequestParams(params);AsyncHttpHelper.post(SERVER_URL, relativeUrl, params, onHttpListner);}}
基础的http请求帮助类
public class AsyncHttpHelper {private static final int HTTP_OK = 200;private static final String HTTP_ERROR = "系统繁忙,请稍后再试";/*** 生成网络错误的json*/private static JSONObject getErrorJson(String error) {JSONObject obj = new JSONObject();try {obj.put("error", error);} catch (JSONException e) {e.printStackTrace();}return obj;}/*** 拼接成完整 http url*/public static String getAbsoluteUrl(String serverName, String relativeUrl) {return ("http://" + serverName + relativeUrl);}/*** 返回基础RequestParams*/public static RequestParams formatRequestParams(RequestParams params) {if (params == null) params = new RequestParams();return params;}/*** 添加ky-value Requestparams*/public static void addRequestParam(RequestParams params, String key, String value) {if (params == null) params = new RequestParams();params.add(key, value);}/*** get 请求,返回json*/public static void get(final String serverName, final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {params = formatRequestParams(params);AsyncHttpClient asyncHttpClient = new AsyncHttpClient();asyncHttpClient.setTimeout(10000);try {asyncHttpClient.get(getAbsoluteUrl(serverName, relativeUrl), params, new JsonHttpResponseHandler("UTF-8") {@Overridepublic void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {super.onFailure(statusCode, headers, responseString, throwable);}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {throwable.printStackTrace();if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));}@Overridepublic void onSuccess(int statusCode, Header[] headers, JSONArray response) {super.onSuccess(statusCode, headers, response);}@Overridepublic void onSuccess(int statusCode, Header[] headers, JSONObject response) {if (onHttpListner != null) {if (statusCode == HTTP_OK) {response = (response == null ? (new JSONObject()) : response);onHttpListner.onHttpListener(true, response);} else onHttpListner.onHttpListener(false, response);}}@Overridepublic void onSuccess(int statusCode, Header[] headers, String responseString) {super.onSuccess(statusCode, headers, responseString);}});} catch (Exception e) {e.printStackTrace();if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));}}/*** post 请求,返回json*/public static void post(final String serverName, final String relativeUrl, RequestParams params, final OnHttpListener<JSONObject> onHttpListner) {params = formatRequestParams(params);AsyncHttpClient asyncHttpClient = new AsyncHttpClient();asyncHttpClient.setTimeout(10000);try {String mm = getAbsoluteUrl(serverName, relativeUrl);asyncHttpClient.post(mm, params, new JsonHttpResponseHandler("UTF-8") {@Overridepublic void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {super.onFailure(statusCode, headers, responseString, throwable);}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {super.onFailure(statusCode, headers, throwable, errorResponse);}@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {throwable.printStackTrace();if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));}@Overridepublic void onSuccess(int statusCode, Header[] headers, JSONArray response) {super.onSuccess(statusCode, headers, response);}@Overridepublic void onSuccess(int statusCode, Header[] headers, JSONObject response) {if (onHttpListner != null) {if (statusCode == HTTP_OK) {response = (response == null ? (new JSONObject()) : response);onHttpListner.onHttpListener(true, response);} else onHttpListner.onHttpListener(false, response);}}@Overridepublic void onSuccess(int statusCode, Header[] headers, String responseString) {super.onSuccess(statusCode, headers, responseString);}});} catch (Exception e) {e.printStackTrace();if (onHttpListner != null) onHttpListner.onHttpListener(false, getErrorJson(HTTP_ERROR));}}/*** get 返回String*/public static void get_AbsoluteUrl_String(final String url, RequestParams params, final OnHttpListener<String> onHttpListner) {params = formatRequestParams(params);AsyncHttpClient asyncHttpClient = new AsyncHttpClient();asyncHttpClient.setTimeout(10000);try {asyncHttpClient.get(url, params, new AsyncHttpResponseHandler() {@Overridepublic void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {if (onHttpListner != null) onHttpListner.onHttpListener(false, arg2 == null ? "error" : new String(arg2));}@Overridepublic void onSuccess(int arg0, Header[] arg1, byte[] arg2) {if (arg0 == HTTP_OK) {if (onHttpListner != null) onHttpListner.onHttpListener(true, arg2 == null ? "error" : new String(arg2));} else {if (onHttpListner != null) onHttpListner.onHttpListener(false, arg2 == null ? "error" : new String(arg2));}}});} catch (Exception e) {e.printStackTrace();if (onHttpListner != null) onHttpListner.onHttpListener(false, "");}}/*** get 返回JSONArray*/public static void get_AbsoluteUrl_JSONArray(final String absoulteUrl, RequestParams params, final OnHttpListener<JSONArray> onHttpListner) {AsyncHttpClient asyncHttpClient = new AsyncHttpClient();asyncHttpClient.setTimeout(10000);asyncHttpClient.get(absoulteUrl, params, new JsonHttpResponseHandler("UTF-8") {@Overridepublic void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {super.onFailure(statusCode, headers, throwable, errorResponse);}@Overridepublic void onSuccess(int statusCode, Header[] headers, JSONArray response) {if (onHttpListner != null) {if (statusCode == HTTP_OK) {response = (JSONArray) (response == null ? (new JSONObject()) : response);onHttpListner.onHttpListener(true, response);} else onHttpListner.onHttpListener(false, response);}}});}/*** post请求*/public static void httpsPost(String url, RequestParams params, final OnHttpListener<String> onHttpListener) {params = formatRequestParams(params);AsyncHttpClient asyncHttpClient = new AsyncHttpClient();KeyStore trustStore = null;try {trustStore = KeyStore.getInstance(KeyStore.getDefaultType());} catch (KeyStoreException e1) {e1.printStackTrace();}try {trustStore.load(null, null);} catch (NoSuchAlgorithmException e1) {e1.printStackTrace();} catch (CertificateException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();}MySSLSocketFactory socketFactory = null;try {socketFactory = new MySSLSocketFactory(trustStore);} catch (KeyManagementException e1) {e1.printStackTrace();} catch (UnrecoverableKeyException e1) {e1.printStackTrace();} catch (NoSuchAlgorithmException e1) {e1.printStackTrace();} catch (KeyStoreException e1) {e1.printStackTrace();}socketFactory.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);asyncHttpClient.setSSLSocketFactory(socketFactory);asyncHttpClient.setTimeout(10000);try {params = formatRequestParams(params);asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {@Overridepublic void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {if (onHttpListener != null) onHttpListener.onHttpListener(false, arg2 == null ? "error" : new String(arg2));}@Overridepublic void onSuccess(int arg0, Header[] arg1, byte[] arg2) {if (arg0 == HTTP_OK) {if (onHttpListener != null) onHttpListener.onHttpListener(true, arg2 == null ? "error" : new String(arg2));} else {if (onHttpListener != null) onHttpListener.onHttpListener(false, arg2 == null ? "error" : new String(arg2));}}});} catch (Exception e) {e.printStackTrace();if (onHttpListener != null) onHttpListener.onHttpListener(false, "");}}/*** 设置持久化保存cookie*/public static void saveCookie(Context context) {AsyncHttpClient asyncHttpClient = new AsyncHttpClient();PersistentCookieStore cookieStore = new PersistentCookieStore(context);asyncHttpClient.setCookieStore(cookieStore);}public static void can() {}/************************************************************ Interface**********************************************************/public interface OnHttpListener<T> {public void onHttpListener(boolean httpSuccessed, T obj);}}