xUtils 工具
方法:
1 /** 2 * 从服务器获取数据 3 */ 4 private void getDataFromServer() { 5 HttpUtils utils = new HttpUtils(); 6 7 //使用xUtils发送请求 8 utils.send(HttpMethod.GET, GlobalContants.CATEGORIES_URL, new RequestCallBack<String>() { 9 10 //访问成功 11 @Override 12 public void onSuccess(ResponseInfo<String> responseInfo) { 13 String result = (String)responseInfo.result; 14 System.out.println(result); 15 parseData(result); 16 17 CacheUtils.setCache(mActivity, GlobalContants.CATEGORIES_URL, result);//设置缓存 18 } 19 20 //访问失败 21 @Override 22 public void onFailure(HttpException error, String msg) { 23 Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show(); 24 25 error.printStackTrace(); 26 27 } 28 29 }); 30 31 }