RequestParams params = new RequestParams(url); params.addQueryStringParameter("username","abc"); params.addQueryStringParameter("password","123"); // 默认缓存存活时间, 单位:毫秒(如果服务器没有返回有效的max-age或Expires则参考) params.setCacheMaxAge(1000 * 60); x.http().get(params, new Callback.CacheCallback<String>() { private boolean hasError = false; private String result = null; @Override public boolean onCache(String result) { //得到缓存数据, 缓存过期后不会进入 this.result = result; return true; //true: 信任缓存数据, 不再发起网络请求; false不信任缓存数据 } @Override public void onSuccess(String result) { //如果服务返回304或onCache选择了信任缓存,这时result为null Log.i("JAVA", "开始请求"); if (result != null) { this.result = result; } } @Override public void onError(Throwable ex, boolean isOnCallback) { hasError = true; Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show(); if (ex instanceof HttpException) { //网络错误 HttpException httpEx = (HttpException) ex; int responseCode = httpEx.getCode(); String responseMsg = httpEx.getMessage(); String errorResult = httpEx.getResult(); //... } else { //其他错误 //... } } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { if (!hasError && result != null) { //成功获取数据 Toast.makeText(x.app(), result, Toast.LENGTH_LONG).show(); } }