Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

xUtils3 功能解析:注解、網路、圖片與資料庫模組

編輯:關於 Android 開發

xUtils3 功能解析:注解、網路、圖片與資料庫模組

xUtils3 是 xUtils 的升級版本,提供注解、網路請求、圖片加載與資料庫操作四大模組,適合需要快速整合常用功能的 Android 專案。

注解模組

在 Application 中呼叫 x.Ext.init(this),即可使用 @ContentView、@ViewInject、@Event 等注解簡化 Activity 與 Fragment 的初始化。

@ContentView(R.layout.fragment_demo)
public class DemoFragment extends Fragment {
    @ViewInject(R.id.btn_fragment)
    private Button button;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }
}

網路模組

支援 GET、POST、檔案下載與大檔案上傳,並可設定保存路徑、斷點續傳與自訂執行緒池。

RequestParams params = new RequestParams("https://api.m.mtime.cn/PageSubArea/TrailerList.api");
x.http().post(params, new Callback.CommonCallback<String>() {
    @Override
    public void onSuccess(String result) {
        textView.setText(result);
    }
});

檔案下載

RequestParams params = new RequestParams(url);
params.setSaveFilePath(Environment.getExternalStorageDirectory() + "/atguigu/480.mp4");
params.setAutoResume(true);
x.http().get(params, new Callback.ProgressCallback<File>() {
    @Override
    public void onLoading(long total, long current, boolean isDownloading) {
        progressbar.setMax((int) total);
        progressbar.setProgress((int) current);
    }
});

大檔案上傳

RequestParams params = new RequestParams(url);
params.setMultipart(true);
params.addBodyParameter("File", new File(path), null, "oppo.mp4");
x.http().post(params, new Callback.ProgressCallback<File>() {
    @Override
    public void onSuccess(File file) {
        Toast.makeText(context, "上傳成功", Toast.LENGTH_SHORT).show();
    }
});

xUtils3 的 API 設計較為簡潔,適合中小型專案快速完成網路與資料處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved