Android網路請求庫RetrofitUtils

來源:互聯網
上載者:User

標籤:void   簡單   avl   ice   功能   rri   create   end   blob   

RetrofitUtils

GitHub地址,幫忙給個Star

項目介紹

Retrofit+Okhttp輔助類的簡單封裝,vesion 1.0.X 實現了Get,Post-Form、Post-Json
三種形式的網路請求,後續版本會實現檔案上傳下載and各類raw的請求方式。

功能
  1. Get
  2. Post表單
  3. PostJson
後續版本待實現功能
  1. 檔案上傳下載(開發中...)
  2. 攔截器
  3. RxJava+Retrofit+Okhttp
  4. 考慮去掉loading(醜且無用),是否把所用到的第三方庫打包進庫中
使用說明1. 引用
    implementation ‘cn.cyq.net:retrofitutils:1.0.3‘    <!--library中引用了下面五個庫,我沒有打包進去了,避免版本衝突,比如七牛雲的okio okhttp衝突-->    //網路請求依賴    implementation ‘com.squareup.okio:okio:1.14.0‘    implementation ‘com.squareup.okhttp3:okhttp:3.10.0‘    implementation ‘com.squareup.retrofit2:retrofit:2.4.0‘    implementation ‘com.squareup.retrofit2:converter-scalars:2.3.0‘    //Loader依賴    implementation ‘com.wang.avi:library:2.1.3‘
2. 初始化

在Application的onCreate()初始化

 public class MyApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        RestClient.init(getApplicationContext(), "baseUrl address");    }}
3. 具體請求Get
 RestClient.buider()                .loader(this)//可以不添加                .url(url)                .success(new ISuccess() {                    @Override                    public void onSuccess(String response) {                        Log.i("test", "GET請求:" + response);                    }                })                .failure(new IFailure() {                    @Override                    public void onFailure() {                        Log.i("test", "失敗");                    }                })                .error(new IError() {                    @Override                    public void onError(int code, String msg) {                        Log.i("test", "錯誤");                    }                })                .build()                .get();
Post表單
RestClient.buider()                .loader(this)                .url(url)                .params("key1", "value1")                .params("key2", "value2")                .success(new ISuccess() {                    @Override                    public void onSuccess(String response) {                        Log.i("test", "POST請求:" + response);                    }                })                .failure(new IFailure() {                    @Override                    public void onFailure() {                        Log.i("test", "失敗");                    }                })                .error(new IError() {                    @Override                    public void onError(int code, String msg) {                        Log.i("test", "錯誤");                    }                })                .build()                .post();
Post Json
        String jsonStr = "{\"username\":\"張三\",\"age\":16}";        RestClient.buider().loader(this)                .url("http://192.168.0.1:8080/service/jsontest.html")                .raw(jsonStr)                .success(new ISuccess() {                    @Override                    public void onSuccess(String response) {                        Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();                        Log.i("test", "Post Row請求:" + response);                    }                })                .error(new IError() {                    @Override                    public void onError(int code, String msg) {                        Log.i("test", "Post Ro請求失敗");                    }                })                .build()                .post();
說明
  1. vesion 1.0.X 是初期版本,不建議在正式項目使用,後續會完善...
引用的庫及版本
ps:2018-07-26[最新]
  1. com.squareup.retrofit2:retrofit:2.4.0
  2. com.squareup.okhttp3:okhttp:3.10.0
  3. com.wang.avi:library:2.1.3
  4. com.squareup.retrofit2:converter-scalars:2.3.0
  5. com.squareup.okio:okio:1.14.0

Android網路請求庫RetrofitUtils

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.