Android -- Final框架之FinalHttp的使用

這裏只展示Final框架中FinalHttp的使用,使用其他的功能可到Github地址參考。

一、jar包

下載地址:鏈接: https://pan.baidu.com/s/1O3JymUfVX_xKeR_IxM5TwA 提取碼: wmfz

Github地址:https://github.com/yangfuhai/afinal

二、使用

權限聲明:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在activity中使用可直接繼承FinalActivity

在fragment中使用在onCreateView中添加

FinalActivity.initInjectedView(this, view);

(1)Get請求

FinalHttp fh = new FinalHttp();
fh.get("網絡請求地址", new AjaxCallBack<Object>() {
    @Override
    public void onLoading(long count, long current) {
        super.onLoading(count, current);
    }

    @Override
    public void onSuccess(Object o) {
        super.onSuccess(o);
        Log.i(TAG, "onSuccess : " + o.toString());
    }

    @Override
    public void onFailure(Throwable t, int errorNo, String strMsg) {
        super.onFailure(t, errorNo, strMsg);
        Log.i(TAG, "onFailure  errorNo : " + errorNo + " --> strMsg : " + strMsg);
    }

    @Override
    public void onStart() {
        super.onStart();
    }
});

(2)Post請求

AjaxParams params = new AjaxParams();
  params.put("username", "michael yang");
  params.put("password", "123456");
  params.put("email", "[email protected]");
  params.put("profile_picture", new File("/mnt/sdcard/pic.jpg")); // 上傳文件
  params.put("profile_picture2", inputStream); // 上傳數據流
  params.put("profile_picture3", new ByteArrayInputStream(bytes)); // 提交字節流
 
  FinalHttp fh = new FinalHttp();
  fh.post("http://www.yangfuhai.com", params, new AjaxCallBack(){
  		@Override
 		public void onLoading(long count, long current) {
 				textView.setText(current+"/"+count);
 		}
 
 		@Override
 		public void onSuccess(String t) {
 			textView.setText(t==null?"null":t);
 		}
  });

(3)下載

FinalHttp fh = new FinalHttp();  
    //調用download方法開始下載
    HttpHandler handler = fh.download("http://www.xxx.com/下載路徑/xxx.apk", //這裏是下載的路徑
    true,//true:斷點續傳 false:不斷點續傳(全新下載)
    "/mnt/sdcard/testapk.apk", //這是保存到本地的路徑
    new AjaxCallBack() {  
                @Override  
                public void onLoading(long count, long current) {  
                     textView.setText("下載進度:"+current+"/"+count);  
                }  
  
                @Override  
                public void onSuccess(File t) {  
                    textView.setText(t==null?"null":t.getAbsoluteFile().toString());  
                }  
  
            });  

	
   //調用stop()方法停止下載
   handler.stop();

The End!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章