bean实体类请求:
1.bean
1 import java.io.Serializable; 2 import com.alibaba.fastjson.annotation.JSONField; 3 ?4 public class UserInfo implements Serializable { 5 ????private static final long serialVersionUID = 145641645L; 6 ?7 ????@JSONField(name = "data") 8 ????private String data; 9 ????@JSONField(name = "error")10 ????private int error;11 ????@JSONField(name = "msg")12 ????private String msg;13 14 ????public UserInfo() {15 ????????super();16 ????}17 18 ????public UserInfo(String data, int error, String msg) {19 ????????super();20 ????????this.data = data;21 ????????this.error = error;22 ????????this.msg = msg;23 ????}24 ????public String getData() {25 ????????return data;26 ????}27 ????public void setData(String data) {28 ????????this.data = data;29 ????}30 ????public int getError() {31 ????????return error;32 ????}33 ????public void setError(int error) {34 ????????this.error = error;35 ????}36 37 ????public String getMsg() {38 ????????return msg;39 ????}40 ????public void setMsg(String msg) {41 ????????this.msg = msg;42 ????}43 44 ????public boolean isSucceed() {45 ????????return 1 == error;46 ????}47 48 }
2.针对bean的request
1 import com.alibaba.fastjson.JSON; 2 import com.yolanda.nohttp.Headers; 3 import com.yolanda.nohttp.JsonObjectRequest; 4 import com.yolanda.nohttp.RequestMethod; 5 import com.yolanda.nohttp.RestRequest; 6 import com.yolanda.nohttp.StringRequest; 7 import com.yolanda.nohttptest.bean.UserInfo; 8 ?9 public class UserInfoRequest extends RestRequest<UserInfo> {10 11 ????public UserInfoRequest(String url, RequestMethod requestMethod) {12 ????????super(url, requestMethod);13 ????}14 ????public UserInfoRequest(String url) {15 ????????super(url);16 ????}17 ????@Override18 ????public String getAccept() {19 ????????return JsonObjectRequest.ACCEPT;20 ????}21 ????@Override22 ????public UserInfo parseResponse(String url, Headers responseHeaders, byte[] responseBody) {23 ????????UserInfo info = null;24 ????????String string = StringRequest.parseResponseString(url, responseHeaders, responseBody);25 ????????try {26 ????????????info = JSON.parseObject(string, UserInfo.class);27 ????????} catch (Exception e) {28 ????????????info = new UserInfo();29 ????????}30 ????????return info;31 ????}32 }
3.具体在activity中使用:
1 public class ObjectActivity extends Activity implements View.OnClickListener { 2 ?3 ????private final int NOHTTP_LOGIN = 0x01; 4 ????private final int NOHTTP_LOGOUT = 0x02; 5 ?6 ????private TextView tvResult; 7 ?8 ????@Override 9 ????protected void onCreate(Bundle savedInstanceState) {10 ????????super.onCreate(savedInstanceState);11 ????????setContentView(R.layout.activity_object);12 ????????findViewById(R.id.btn_login).setOnClickListener(this);13 ????????findViewById(R.id.btn_logout).setOnClickListener(this);14 ????????tvResult = (TextView) findViewById(R.id.tv_result);15 ????}16 17 ????@Override18 ????public void onClick(View v) {19 ????????if (v.getId() == R.id.btn_login) {20 ????????????UserInfoRequest request = new UserInfoRequest(Constants.LOGIN, RequestMethod.GET);21 ????????????request.add("userName", "yolanda");22 ????????????request.add("userPwd", "123");23 ????????????CallServer.getInstance().add(this, request, callBack, NOHTTP_LOGIN, true, false, true);24 ????????} else {25 ????????????UserInfoRequest request = new UserInfoRequest(Constants.LOGOUT, RequestMethod.GET);26 ????????????CallServer.getInstance().add(this, request, callBack, NOHTTP_LOGOUT, true, false, true);27 ????????}28 ????}29 30 ????private HttpCallBack<UserInfo> callBack = new HttpCallBack<UserInfo>() {31 32 ????????@Override33 ????????public void onSucceed(int what, Response<UserInfo> response) {34 ????????????UserInfo info = response.get();35 ????????????if (what == NOHTTP_LOGIN) {// 处理登录结果36 ????????????????if (info.isSucceed()) {37 ????????????????????tvResult.setText("登录接口结果:" + info.getData());38 ????????????????}39 ????????????} else if (what == NOHTTP_LOGOUT) {// 处理登出结果40 ????????????????if (info.isSucceed()) {41 ????????????????????tvResult.setText("退出接口结果:" + info.getData());42 ????????????????}43 ????????????}44 ????????}45 46 ????????@Override47 ????????public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {48 ????????????tvResult.setText("请求失败");49 ????????}50 ????};51 }
NoHttp封装--02
原文地址:https://www.cnblogs.com/ganchuanpu/p/9029233.html