NoHttpActivity

 public class NoHttpActivity extends Activity implements View.OnClickListener {

     private final int NOHTTP_LOGIN = 0x01;//登陆
private final int NOHTTP_LOGOUT = 0x02;//退出 private TextView tvResult; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nohttp);
findViewById(R.id.btn_login).setOnClickListener(this);
findViewById(R.id.btn_logout).setOnClickListener(this);
tvResult = (TextView) findViewById(R.id.tv_result);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_login) {
FastJsonRequest request = new FastJsonRequest(Constants.LOGIN, RequestMethod.GET);
request.add("userName", "yolanda");
request.add("userPwd", "");
CallServer.getInstance().add(this, request, callBack, NOHTTP_LOGIN, true, false, true);
} else {
FastJsonRequest request = new FastJsonRequest(Constants.LOGOUT, RequestMethod.GET);
CallServer.getInstance().add(this, request, callBack, NOHTTP_LOGOUT, true, false, true);
}
} private HttpCallBack<JSONObject> callBack = new HttpCallBack<JSONObject>() { @Override
public void onSucceed(int what, Response<JSONObject> response) {
if (what == NOHTTP_LOGIN) {// 处理登录结果
JSONObject jsonObject = response.get();
tvResult.setText("登录接口数据:" + jsonObject.getString("data"));
} else if (what == NOHTTP_LOGOUT) {// 处理登出结果
JSONObject jsonObject = response.get();
tvResult.setText("退出接口数据:" + jsonObject.getString("data"));
}
} @Override
public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
tvResult.setText("请求失败");
}
}; }

CallServer:

 public class CallServer {

     private static CallServer instance;

     private RequestQueue queue;

     public synchronized static CallServer getInstance() {
if (instance == null) {
instance = new CallServer();
}
return instance;
} private CallServer() {
queue = NoHttp.newRequestQueue();
} /**
* 添加一个请求到请求队列
*
* @param context 上下文
* @param request 请求对象
* @param callBack 接受回调结果
* @param what what,当多个请求用同一个responseListener接受结果时,用来区分请求
* @param isShowDialog 是否显示dialog
* @param isCanCancel 请求是否能被用户取消
* @param isShowError 是否提示用户错误信息
*/
public <T> void add(Context context, Request<T> request, HttpCallBack<T> callBack, int what, boolean isShowDialog, boolean isCanCancel, boolean isShowError) {
queue.add(what, request, new ResponseListener<T>(request, context, callBack, isShowDialog, isCanCancel, isShowError));
} }

ResponseListener:

 public class ResponseListener<T> implements OnResponseListener<T> {

     private Request<T> mRequest;

     private WaitDialog mDialog;

     private HttpCallBack<T> callBack;

     private boolean isShowError;

     public ResponseListener(Request<T> request, Context context, HttpCallBack<T> callBack, boolean isShowDialog, boolean isCanCancel, boolean isShowError) {
this.mRequest = request;
this.callBack = callBack;
this.isShowError = isShowError;
if (context != null && isShowDialog) {
mDialog = new WaitDialog(context);
mDialog.setCancelable(isCanCancel);
mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mRequest.cancel(true);
}
});
}
} @Override
public void onStart(int what) {
if (mDialog != null && !mDialog.isShowing())
mDialog.show();
} @Override
public void onSucceed(int what, Response<T> response) {
if (callBack != null)
callBack.onSucceed(what, response);
} @Override
public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
if (isShowError) {
if (exception instanceof ClientError) {// 客户端错误
Toast.show("客户端发生错误");
} else if (exception instanceof ServerError) {// 服务器错误
Toast.show("服务器发生错误");
} else if (exception instanceof NetworkError) {// 网络不好
Toast.show("请检查网络");
} else if (exception instanceof TimeoutError) {// 请求超时
Toast.show("请求超时,网络不好或者服务器不稳定");
} else if (exception instanceof UnKnownHostError) {// 找不到服务器
Toast.show("未发现指定服务器");
} else if (exception instanceof URLError) {// URL是错的
Toast.show("URL错误");
} else if (exception instanceof NotFoundCacheError) {
Toast.show("没有发现缓存");
} else {
Toast.show("未知错误");
}
}
if (callBack != null)
callBack.onFailed(what, url, tag, exception, responseCode, networkMillis);
} @Override
public void onFinish(int what) {
if (mDialog != null && mDialog.isShowing())
mDialog.dismiss();
} }

HttpCallBack

 public interface HttpCallBack<T> {

     /**
* Server correct response to callback when an HTTP request.
*
* @param what the credit of the incoming request is used to distinguish between multiple requests.
* @param response in response to the results.
*/
void onSucceed(int what, Response<T> response); /**
* When there was an error correction.
*
* @param what the credit of the incoming request is used to distinguish between multiple requests.
* @param url url.
* @param tag tag of request callback.
* @param exception error message for request.
* @param responseCode server response code.
* @param networkMillis request process consumption time.
*/
void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis); }

FastJsonRequest:自定义FastJsonRequest对象,所有的自定义对象都要继承{@link RestReqeust}

 public class FastJsonRequest extends RestRequest<JSONObject> {

     public FastJsonRequest(String url, RequestMethod requestMethod) {
super(url, requestMethod);
} public FastJsonRequest(String url) {
super(url);
} /**
* 高速服务端你能接受的数据类型是什么
*/
@Override
public String getAccept() {
return JsonObjectRequest.ACCEPT;
} /**
* @param url 请求的url
* @param responseHeaders 服务端的响应头
* @param 服务端的响应数据
* @return 你解析后的对象
*/
@Override
public JSONObject parseResponse(String url, Headers responseHeaders, byte[] responseBody) {
return parse(url, responseHeaders, responseBody);
} /**
* 解析服务端数据成{@link JsonObject}
*
* @param url
* @param responseHeaders
* @param responseBody
* @return
*/
public static JSONObject parse(String url, Headers responseHeaders, byte[] responseBody) {
String string = StringRequest.parseResponseString(url, responseHeaders, responseBody);
JSONObject jsonObject = null;
try {
jsonObject = JSON.parseObject(string);
} catch (Exception e) {// 可能返回的数据不是json,或者其他异常
string = "{}";
jsonObject = JSON.parseObject(string);
}
return jsonObject;
} }

WaitDialog:

 public class WaitDialog extends ProgressDialog {

     public WaitDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setCanceledOnTouchOutside(false);
setProgressStyle(STYLE_SPINNER);
setMessage("正在请求,请稍候…");
} }

Constants

public class Constants {

    private static final String SERVER = "http://192.168.1.116/HttpServer/";

    /**
* 登录接口
*/
public static final String LOGIN = SERVER + "login"; /**
* 退出登录接口
*/
public static final String LOGOUT = SERVER + "logout"; }

最新文章

  1. python基础知识理解
  2. Nginx 下配置SSL证书的方法
  3. asp.net GDI+绘制矩形渐变
  4. 一个基于POP3协议进行邮箱账号验证的类
  5. 夺命雷公狗ThinkPHP项目之----企业网站20之网站前台头尾分离
  6. pure virtual、impure virtual、non-virtual函数的接口继承和实现继承
  7. Mahout分布式运行实例:基于矩阵分解的协同过滤评分系统(一个命令实现文件格式的转换)
  8. overflow:hidden 你所不知道的事
  9. Spring报错——Scope &#39;session&#39; is not active for the current thread
  10. sql: 左连接 和内连接区别联系
  11. bzoj 4945: [Noi2017]游戏
  12. HTTP引流神器Goreplay详解【官译】
  13. STL中vector、set、list和map
  14. Java复习总结——详细理解Java反射机制
  15. Bitcoin Core钱包客户端的区块数据搬家指南
  16. django 数据库查询的几个知识点
  17. Daily Scrum 10.22
  18. 【linux】linux 下 shell命令 执行结果赋值给变量【两种方式】
  19. iOS - OC - JSON 解析 - NSJSONSerialization
  20. Js获取上一月份

热门文章

  1. OpenStack-Ocata版+CentOS7.6 云平台环境搭建 —7.网络服务Neutron配置
  2. [git] 文件操作
  3. Testing - 软件测试知识梳理 - 相关词汇
  4. 机器学习基石笔记:13 Hazard of Overfitting
  5. centos7使用lldb调试netcore应用转储dump文件
  6. python(leetcode)-350两个数组的交集
  7. Java架构技术进阶之:从分布式到微服务,深挖Service Mesh
  8. “玲珑杯”ACM比赛 Round #18---图论你先敲完模板(DP+思维)
  9. HDU 4570---Multi-bit Trie(区间DP)
  10. 读书笔记(06) - 语法基础 - JavaScript高级程序设计