bean實體類請求: 1.bean 2.針對bean的request 3.具體在activity中使用: 可以解析所有JavaBean的自定義請求 activity ...
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 @Override 18 public String getAccept() { 19 return JsonObjectRequest.ACCEPT; 20 } 21 @Override 22 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 @Override 18 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 @Override 33 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 @Override 47 public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) { 48 tvResult.setText("請求失敗"); 49 } 50 }; 51 }
可以解析所有JavaBean的自定義請求
1 public class JavaBeanRequest<E> extends RestRequest<E> { 2 3 private Class<E> clazz; 4 5 public JavaBeanRequest(String url, RequestMethod requestMethod, Class<E> clazz) { 6 super(url, requestMethod); 7 this.clazz = clazz; 8 } 9 10 public JavaBeanRequest(String url, Class<E> clazz) { 11 this(url, RequestMethod.GET, clazz); 12 } 13 14 @Override 15 public E parseResponse(String url, Headers responseHeaders, byte[] responseBody) { 16 String result = StringRequest.parseResponseString(url, responseHeaders, responseBody); 17 try { 18 return JSON.parseObject(result, clazz); 19 } catch (Exception e) { 20 E instance = null; 21 try { 22 // 但是前提是傳進來的JavaBean必須提供了預設實現 23 instance = clazz.newInstance(); 24 } catch (InstantiationException e1) { 25 } catch (IllegalAccessException e1) { 26 } 27 return instance; 28 } 29 } 30 31 @Override 32 public String getAccept() { 33 return JsonObjectRequest.ACCEPT; 34 } 35 36 }
activity
Request<ServerBean> request = new JavaBeanRequest<ServerBean>(url, ServerBean.class); CallServer.getInstance().add(this, request, callBack, 0, true, false, true);