一、簡介 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); }五、結尾
輪子常有,好輪子也不少,本身造的,分外稀罕。 異步