Android network request library RetrofitUtils

RetrofitUtils

GitHub address, give Star a hand

Project introduction

It is a simple encapsulation of Retrofit+Okhttp auxiliary class. vesion 1.0.X implements Get, post form and post JSON
There are three forms of network requests, and the subsequent versions will realize file upload and download and various raw requests.

function

  1. Get
  2. Post form
  3. PostJson

Functions to be implemented in subsequent versions

  1. File upload and download (under development...)
  2. Interceptor
  3. RxJava+Retrofit+Okhttp
  4. Consider removing loading (ugly and useless) and whether to package the third-party library used into the library

Instructions

1. Quotes
    implementation 'cn.cyq.net:retrofitutils:1.0.3'
    <!--library The following five libraries are referenced in. I didn't pack them in to avoid version conflicts,For example, qiniuyun's okio okhttp conflict-->
    //Network request dependency
    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 dependence
    implementation 'com.wang.avi:library:2.1.3'
2. initialization

Initialization in onCreate() of Application

 public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        RestClient.init(getApplicationContext(), "baseUrl address");
    }
}
3. Specific request
Get
 RestClient.buider()
                .loader(this)//Can not add
                .url(url)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        Log.i("test", "GET Request:" + response);
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Log.i("test", "fail");
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.i("test", "error");
                    }
                })
                .build()
                .get();
Post form
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 Request:" + response);
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Log.i("test", "fail");
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.i("test", "error");
                    }
                })
                .build()
                .post();
Post Json
        String jsonStr = "{\"username\":\"Zhang San\",\"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 Request:" + response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.i("test", "Post Ro request was aborted");
                    }
                })
                .build()
                .post();

Explain

  1. vesion 1.0.X is the initial version. It is not recommended to be used in formal projects. It will be improved later

Reference library and version

PS: July 26, 2018 [latest]
  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

Tags: Android OkHttp Retrofit JSON network

Posted on Fri, 31 Jan 2020 05:10:31 -0500 by lizzardnub