第一个androidAPP项目总结—数据请求

  1. 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);
}
});

 

  1. 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();
  }
}
 
 
  1. 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;
}

}

 

转载于:https://www.cnblogs.com/minghualiyan/p/5120540.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值