转载请标明出处:
http://blog.csdn.net/lxk_1993/article/details/50897613
本文出自:【lxk_1993的博客】;
xUtils 的 post和get 请求 使用记录.最后有规范的代码。开始的只是过程
post请求:
刚开始不知道怎么添加请求参数,就自己动手尝试。最后通过下面的代码请求成功了。
RequestParams params = new RequestParams(HttpUrl.add_to_cars);//里面参数为请求地址 // 请求参数 // { // "memberId": "297ebe0e528b62cd01528c36ed02005c", // "commCommodityId": "2c95869052f8659c0152f865fa940000", // "commCount": "20" // }
//直接使用了拼接字符串。 params.setBodyContent("{\"memberId\":\"" + "297ebe0e528b62cd01528c36ed02005c" + "\",\"commCommodityId\":\"" + goodsId + "\",\"commCount\":\"" + String.valueOf(chooseCount) + "\"}"); Callback.Cancelable cancelable = x.http().post(params, new Callback.CommonCallback<TestBean>() { @Override public void onSuccess(TestBean baseBean) { showToast(baseBean.message); } @Override public void onError(Throwable ex, boolean b) { Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show(); } @Override public void onCancelled(CancelledException e) { // Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show(); } @Override public void onFinished() { } });
TestBean.java
@HttpResponse(parser = JsonResponseParser.class) public class TestBean extends BaseBean { /** * data : null */ public Object data; }BaseBean.javapublic class BaseBean implements Parcelable { /** * message : * count : 3 * curCount : 0 * success : true */ public String message; public int count; public int curCount; public boolean success; @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(this.message); dest.writeInt(this.count); dest.writeInt(this.curCount); dest.writeByte(success ? (byte) 1 : (byte) 0); } public BaseBean() { } protected BaseBean(Parcel in) { this.message = in.readString(); this.count = in.readInt(); this.curCount = in.readInt(); this.success = in.readByte() != 0; } public static final Parcelable.Creator<BaseBean> CREATOR = new Parcelable.Creator<BaseBean>() { public BaseBean createFromParcel(Parcel source) { return new BaseBean(source); } public BaseBean[] newArray(int size) { return new BaseBean[size]; } }; }
get方式:
get方式比较简单,只需要把下面的url改成你的请求地址,把Object 改成请求返回的Json数据实体类就行了。
还有就是记得在改的实体类之前加上这个@HttpResponse(parser = JsonResponseParser.class)。例子如下
@HttpResponse(parser = JsonResponseParser.class) public class GoodsDetailsBean extends BaseBeanget请求例子
RequestParams params = new RequestParams(url);//url是get的请求地址 Callback.Cancelable cancelable = x.http().get(params, new Callback.CommonCallback<Object>() { @Override public void onSuccess(Object o) { //请求成功 } @Override public void onError(Throwable throwable, boolean b) {//请求出错
} @Override public void onCancelled(CancelledException e) {//请求取消
} @Override public void onFinished() {//请求完成
}});
后面发现有规范的代码如下。
public class HttpUtils { // get请求方式 public static void getDataFromWeb(Callback.CommonCallback callback, String url) { RequestParams params = new RequestParams(url); x.http().get(params, callback); } // post请求方式 public static void postDataFromWeb(Object entity, Callback.CommonCallback callback, String url) { RequestParams params = new RequestParams(url); String str = JsonBinder.buildNonNullBinder().toJson(entity); params.setAsJsonContent(true); params.addBodyParameter("", str, "application/json"); x.http().post(params, callback); } }get请求就可以改为这样,Object为请求返回的实体类,记得在改的实体类之前加上这个@HttpResponse(parser = JsonResponseParser.class)。url为请求地址.HttpUtils.getDataFromWeb(new Callback.CommonCallback<Object>() { @Override public void onSuccess(Object bean) { } @Override public void onError(Throwable throwable, boolean b) { } @Override public void onCancelled(CancelledException e) { } @Override public void onFinished() { } }, url);post请求可以改成这样.object和url和上面一样。PostBean是post请求参数组成的一个自定义的请求类。只要把请求参数传进这个类的构造方法里面去就好了
PostBean bean = new PostBean("?","?","?");HttpUtils.postDataFromWeb(bean, new Callback.CommonCallback<Object>() { @Override public void onSuccess(Object o) { } @Override public void onError(Throwable throwable, boolean b) { } @Override public void onCancelled(CancelledException e) { } @Override public void onFinished() { } }, url);class PostBean { String ?; String ?; String ?; public PostBean(String ?, String ?, String ?) { this.?= ?; this.?= ?; this.?= ?; } }