- 1.使用
ShenBuLuoHttpImpl.getMHttpImpl(context).getAddressList(mod.getCouponCode(), new HttpAfter() {
@Override
public void afterSuccess(Result resultBean) {
//数据解析
Object list = resultBean.getResult();
KaQuanMod[] mlist = JsonUtils
.getObjectMapper().convertValue(
list,
KaQuanMod[].class);
notifyDataSetChanged();
}
@Override
public void afterFail(Result resultBean) {
//mSwipeLayout.setRefreshing(false);
}
@Override
public void afterError(Result resultBean) {
//mSwipeLayout.setRefreshing(false);
}
});
- 2.ShenBuLuoHttpImpl类
public class ShenBuLuoHttpImpl extends BaseHttpImpl {
private static ShenBuLuoHttpImpl mHttpImpl;//单例的接口处理类
private ShenBuLuoHttpImpl(Context con) {
super(con);
mContext = con;
}
/**
* 获取接口处理类
*
* @param con
* @return
*/
public static ShenBuLuoHttpImpl getMHttpImpl(Context con) {
if (mHttpImpl == null) {
mHttpImpl = new ShenBuLuoHttpImpl(con);
}
return mHttpImpl;
}
public void getAddressList(final HttpAfter afterHttp) {
if (AppDataUtils.userId == null) {
showToast("请登录");
return;
}
new HttpHelper.Builder().toUrl(URLUtils.GET_ADDRESS)
.addParams("userId", AppDataUtils.userId)//用户ID
.addParams("token", AppDataUtils.token)
.executeGet(new AsyncHandlerTextBase() {
@Override
public void onSuccess(int statusCode, Header[] headers, String result) {
super.onSuccess(statusCode, headers, result);
//解析
final Result resultBean = JsonUtils.readValue(result, Result.class);
switch (Integer.valueOf(resultBean.getCode())) {
case URLUtils.RESULT_SUCCESS:
afterHttp.afterSuccess(resultBean);
break;
case URLUtils.RESULT_FAILED:
afterHttp.afterFail(resultBean);
//请求数据失败
showToast(resultBean.getReason());
break;
case URLUtils.RESULT_ERROR:
afterHttp.afterError(resultBean);
//服务器崩溃
showServerErrorMsg(resultBean);
break;
default:
// 其他情况处理,一般弹出原因
showOtherErrorMsg(resultBean);
break;
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String result, Throwable
throwable) {
showToast(mContext.getString(R.string.server_busy));
}
}
).build();
}
}
- 3.HTTPHelper类
package com.puyuntech.sixcontry.http;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
* Created by Nicodelee on 15/6/15.
*/
public class HttpHelper {
private RequestParams params;
private String url;
private AsyncHttpResponseHandler responseHandler;
public static class Builder {
private String url = "";
private RequestParams params = null;
private AsyncHttpResponseHandler responseHandler = null;
public Builder(){
}
public Builder toUrl(String burl){
url = burl;
return this;
}
public Builder addParams(String key, String value) {
if (params == null) {
params = new RequestParams();
}
params.put(key,value);
return this;
}
public Builder executeGet( AsyncHttpResponseHandler responseHandler){
AsnyClient.get(url, params, responseHandler);
return this;
}
public Builder executePost( AsyncHttpResponseHandler responseHandler){
AsnyClient.post(url, params, responseHandler);
return this;
}
public HttpHelper build() {
return new HttpHelper(this);
}
}
private HttpHelper(Builder builder) {
params = builder.params;
url = builder.url;
responseHandler = builder.responseHandler;
}
}