标签:api 扩展 demo 实体 led 字符 nic space stact
首先在这里本片文章是以网络通信封装为主,而app开发首先重要就是网络通信,而如今主流的async、volley、okhttp等,阿么这么网络库怎样能做到更好封装、更好的切换,从而不影响业务层的代码,而本文就是基于这个思路,从而封装了okhttp,并能支持封装切换到async、或者volley,后期会支持这两个库的全部特性。并在调用时候无影响。当前版本号2.6。github地址:
https://github.com/apple317/HttpClientUtils
眼下支持
Android Studio
使用前,对于Android Studio的用户,能够选择加入:
compile project(‘:HttpClientUtils‘)
HttpConfiguration.Builder configuration=new HttpConfiguration.Builder(getApplicationContext()); configuration.connectTimeout(2000); configuration.retryOnConnectionFailure(true); configuration.readTimeout(2000); configuration.writeTimeout(2000); configuration.diskCacheSize(1000 * 1024); configuration.diskCacheDir(getCacheDir()); BaseHttpClient.getBaseClient().init(configuration.build());里面设置超时、读写时间、缓冲路径等
/** * 普通參数请求方式 * url 传入网络地址 * put传入值 * tag设置tag */ public void getParam(View view) { BaseHttpClient.getBaseClient().newBuilder() .url("http://api.dianping.com/v1/metadata/get_cities_with_deals") .put("appkey", "56065429") .setParse(UserBean.class) .put("sign", "AF24BF8A3F31D22D25422BCDD86AA322F43B5BAB") .setTag("deals").build().execute(new HttpCallback() { @Override public void onSuccess(String content, BaseHttpClient object, Object parse) { //刷新ui操作 } @Override public void onError(Throwable error, BaseHttpClient client) { } }); }
在demo中PostActivity中的postStringData方法:
/** * post上传字符串 * addUrl传入url地址 * put传入參数能够 * apple_txt 这个是专门用来传入文本上传的,请注意 */ public void postStringData(View view) { /** * 第一种写法 */ BaseHttpClient.getBaseClient().newBuilder().url("url") .content("你好好好").method(METHOD.POST_STRING) .build().execute(new HttpCallback() { @Override public void onSuccess(String content, BaseHttpClient object, Object parse) { //刷新ui } @Override public void onError(Throwable error, BaseHttpClient client) { } }); }
/** * 普通post请求数据 * 默认以表单提交 * addUrl传入url地址 * put传入參数能够 *setTag 设置tag */ public void postData(View view) { /** * 第一种写法 */ BaseHttpClient.getBaseClient().newBuilder() .url("url") .put("device_id", "1236716") .setTag("postactivity").put("os", "2") .put("version", "2.1.0"). put("mobile", "13621937708").method(METHOD.POST_FORM) .build().execute(new HttpCallback() { @Override public void onSuccess(String content, BaseHttpClient object, Object parse) { //刷新UI } @Override public void onError(Throwable error, BaseHttpClient client) { } }); }
在demo中PostActivity中的postFormFile方法:
/** * post * 表单文件上传 * 默认以表单提交 * addUrl传入url地址 * put传入參数能够 *setTag 设置tag * put 传入logo为文件上传定义接口名称 value:为文件对象 * setBaseParams 通过这种传入參数对象 * 上传文件onProgress 会返回当前网络上传的进度 * POST_FORM 是普通表单提交 */ public void postFormFile(View view) { try { File file=new File("path"); ArrayList<File> arrayList=new ArrayList<File>(); arrayList.add(file); // File file = new File(Environment.getExternalStorageDirectory() // , "4cc75752fa532553bf7b6f7e00f26db8.png"); BaseHttpClient.getBaseClient().newBuilder() .url("url") .put("game", "lol") .put("logo", arrayList) .put("token","ee595bd5078a6e67a110c6bd8828c8e2a2388c12") .put("version", "2.1.0") .put("device_id","867905026687709") .put("os","2") .method(METHOD.POST_FORM_FILE).build() .execute(new HttpCallback() { @Override public void onSuccess(String content, BaseHttpClient object, Object parse) { //刷新ui } @Override public void onError(Throwable error, BaseHttpClient client) { } }); } catch (Exception e) { e.printStackTrace(); } }文件上传进度
在demo中PostActivity中postFormFileProgress方法:
/** * post * 表单文件上传 * 默认以表单提交 * addUrl传入url地址 * put传入參数能够 *setTag 设置tag * put 传入logo为文件上传定义接口名称 value:为文件对象 也能够为文件数组对象 * setBaseParams 通过这种传入參数对象 * 类型一定要传入POST_FORM_PROGRESS * 上传文件onProgress 会返回当前网络上传的进度 */ public void postFormFileProgress(View view) { try { File file = new File(Environment.getExternalStorageDirectory() , "4cc75752fa532553bf7b6f7e00f26db8.png"); BaseHttpClient.getBaseClient().newBuilder().url("url") .put("game", "lol") .put("logo", file) .put("token","ee595bd5078a6e67a110c6bd8828c8e2a2388c12") .put("version", "2.1.0") .put("device_id","867905026687709") .put("os","2") .method(METHOD.POST_FORM_PROGRESS).build() .execute(new UploadCallback() { @Override public void onSuccess(String content, BaseHttpClient object, Object parse) { } @Override public void onError(Throwable error, BaseHttpClient client) { } @Override public void uploadProgress(long bytesRead, long contentLength, boolean done) { mProgressBar.setProgress((int) (100 * (float)bytesRead * 1.0f / contentLength)); } }); } catch (Exception e) { e.printStackTrace(); } }
文件下载进度
/** * addUrl 加入url地址 * put传入參数能够 *setTag 设置tag * downName 下载文件命名 * downDir 下载文件文件夹 假设不传会默认生成一个getCacheDirectory * DownEntity 下载返回数据实体: * public String name;//名称 public String path;//保存地址 public String url; public int httpCode;//http网络状态 //下载是否完毕 public boolean statue;//下载是否完毕 public long currentByte;//下载当前字节数 public long totalByte;//下载总字节数 public boolean isExecuted;//下载是否运行 public String dir;//下载文件夹 public boolean isCanceled;//下载是否取消 public String message;//下载返回消息 * @param view */ public void downData(View view){ /** * 第一种写法 */ BaseHttpClient.getBaseClient().newBuilder().url("http://112.65.235.160/vlive.qqvideo.tc.qq.com/m0019469p4a.mp4?vkey=4C7F305D62ABA38AF8BF474C40A0DF9700C8F07BF29BE26D76F17F8A7E73B9FEB1424CC479C4C863BFBDD095AA5EBE49A0CDE3EAEB32E2AD0C009E7C5B37521C0912AF6905C70C601471E664777B9C38C726B03E8D193D62&br=34&platform=2&fmt=msd&sdtfrom=v3010&type=mp4&locid=89489e75-bb18-40e4-989b-89d6b34adf32&size=56306437&ocid=1362567084") .downName("apple_nba").method(METHOD.DOWNLOAD_FILE).build().execute(new DownCallback() { @Override public void downProgress(DownEntity entity) { Log.i("HU", "======bytes===" + entity.currentByte + "==contenLength==" + entity.totalByte); id_progress.setProgress((int) (100 * (float) entity.currentByte * 1.0f / entity.totalByte)); txt_content.setText("当前下载的文件文件夹是"+entity.path+"文件名:"+entity.name+"网络返回code"+entity.httpCode+"===服务端返回消息=="+entity.message); } }); }
BaseHttpClient.getBaseClient().cancelTag("url");
BaseHttpClient.getBaseClient() .setTag("deals")
mParams.setTag("tag");
比方,在请求中传入tag对象,销魂时候能够统一销毁同一tag对象訪问
@Override
protected void onDestroy()
{
super.onDestroy();
//能够取消同一个tag的
BaseHttpClient.getBaseClient(getApplicationContext()).cancelTag("tag对象");
}
ConcurrentHashMap<String, String> headMap=new ConcurrentHashMap<String, String>(); headMap.put("device","1231231241"); BaseHttpClient.getBaseClient().newBuilder() .url("http://api.dianping.com/v1/metadata/get_cities_with_deals") .put("appkey", "56065429") .setParse(UserBean.class) .setHeadMap(headMap) .put("sign", "AF24BF8A3F31D22D25422BCDD86AA322F43B5BAB") .setTag("deals").build().execute(new HttpCallback() { @Override public void onSuccess(String content, BaseHttpClient object, Object parse) { UserBean userBean=(UserBean)parse; txt_content.setText(userBean.getCities().get(0) + "type===" ); } @Override public void onError(Throwable error, BaseHttpClient client) { } });
标签:api 扩展 demo 实体 led 字符 nic space stact
原文地址:http://www.cnblogs.com/lxjshuju/p/7353644.html