一、简介 java
dazzle.common.http是一个Android访问网络的http工具。其中主要有:get方法请求、post方法请求、文件下载、文件上传等功能。主要是封装了HttpClient。参考框架,Afinal 网络
二、get方法请求使用 框架
/** * 异步GET测试 * * @param textView */ public static void getTest(final TextView textView) { HttpUtils httpUtils = new HttpUtils(); RequestParams requestParams = new RequestParams(new Object[] { "name", "xuan", "age", "7" }); httpUtils.get("http://192.168.0.103/test/test.htm", requestParams, new RequestCallBack<String>() { @Override public void onSuccess(String t) { textView.setText(t); } }); }
/** * 同步GET测试,耗时操做,要配合线程使用 * * @return */ public static void getSyncTest(final TextView textView) { HttpUtils httpUtils = new HttpUtils(); RequestParams requestParams = new RequestParams(); requestParams.put("name", "xuan"); requestParams.put("age", "7"); textView.setText((String) httpUtils.getSync("http://192.168.0.103/test/test.htm", requestParams)); }三、post方法请求使用
/** * 同步POST测试,耗时操做,要配合线程使用 * * @return */ public static void postSyncTest(final TextView textView) { HttpUtils httpUtils = new HttpUtils(); HashMap<String, String> paramMap = new HashMap<String, String>(); paramMap.put("name", "xuan"); paramMap.put("age", "7"); RequestParams requestParams = new RequestParams(paramMap); textView.setText((String) httpUtils.postSync("http://192.168.0.103/test/test.htm", requestParams)); }
/** * 异步POST测试 * * @param textView */ public static void postTest(final TextView textView) { HttpUtils httpUtils = new HttpUtils(); RequestParams requestParams = new RequestParams("name", "xuan"); requestParams.put("age", "7"); httpUtils.post("http://192.168.0.103/test/test.htm", requestParams, new RequestCallBack<String>() { @Override public void onSuccess(String t) { textView.setText(t); } }); }三、下载文件API使用
public static void downloadTest(final TextView textView) { String target = Environment.getExternalStorageDirectory().getPath() + "/xuan/1.jpg";// 文件存放位置 RequestCallBack<File> requestCallBack = new RequestCallBack<File>() { @Override public void onStart() { textView.setText("文件开始下载"); } @Override public void onLoading(long count, long current) { textView.setText("文件下载进度:" + current + "/" + count); } @Override public void onSuccess(File t) { textView.setText("文件下载成功"); } @Override public void onFailure(Throwable t, int errorNo, String strMsg) { textView.setText("文件下失败:Throwable[" + t.getMessage() + "]errorNo[" + errorNo + "]strMsg[" + strMsg + "]"); } }; // 大大的注意,只有设置了这个方法,onLoading才会被调用有效,单位ms requestCallBack.progress(true, 10); HttpUtils httpUtils = new HttpUtils(); httpUtils .download( "http://h.hiphotos.baidu.com/album/w%3D2048/sign=bcc2b1908601a18bf0eb154faa170608/42166d224f4a20a471350b7b91529822720ed066.jpg", target, requestCallBack); }四、上传文件API使用
public static void uploadFileTest(final TextView textView) { File file = new File(Environment.getExternalStorageDirectory().getPath() + "/xuan/1.jpg"); RequestParams requestParams = new RequestParams(); requestParams.put("name", "xuan");// 普通参数 try { requestParams.put("file", file);// 文件参数 } catch (Exception e) { textView.setText("文件不存在"); } RequestCallBack<String> requestCallBack = new RequestCallBack<String>() { @Override public void onStart() { textView.setText("文件开始上传"); } @Override public void onLoading(long count, long current) { textView.setText("文件上传进度:" + current + "/" + count); } @Override public void onSuccess(String t) { textView.setText(t); } @Override public void onFailure(Throwable t, int errorNo, String strMsg) { textView.setText("文件上传失败:Throwable[" + t.getMessage() + "]errorNo[" + errorNo + "]strMsg[" + strMsg + "]"); } }; requestCallBack.progress(true, 10); HttpUtils httpUtils = new HttpUtils(); httpUtils.post("http://192.168.0.103/test/test.htm", requestParams, requestCallBack); }五、结尾
轮子常有,好轮子也不少,本身造的,分外稀罕。 异步