Svr-Logansquare

A simple json request use volley.Svr use builder pattern.

License

License

Categories

Categories

Square Business Logic Libraries Financial LoganSquare Data JSON
GroupId

GroupId

com.github.rayboot.svr
ArtifactId

ArtifactId

svr-logansquare
Last Version

Last Version

1.0.3
Release Date

Release Date

Type

Type

aar
Description

Description

Svr-Logansquare
A simple json request use volley.Svr use builder pattern.
Project URL

Project URL

https://github.com/rayboot/SimpleVolleyRequest
Source Code Management

Source Code Management

https://github.com/rayboot/SimpleVolleyRequest

Download svr-logansquare

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.rayboot.svr/svr-logansquare/ -->
<dependency>
    <groupId>com.github.rayboot.svr</groupId>
    <artifactId>svr-logansquare</artifactId>
    <version>1.0.3</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.rayboot.svr/svr-logansquare/
implementation 'com.github.rayboot.svr:svr-logansquare:1.0.3'
// https://jarcasting.com/artifacts/com.github.rayboot.svr/svr-logansquare/
implementation ("com.github.rayboot.svr:svr-logansquare:1.0.3")
'com.github.rayboot.svr:svr-logansquare:aar:1.0.3'
<dependency org="com.github.rayboot.svr" name="svr-logansquare" rev="1.0.3">
  <artifact name="svr-logansquare" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.rayboot.svr', module='svr-logansquare', version='1.0.3')
)
libraryDependencies += "com.github.rayboot.svr" % "svr-logansquare" % "1.0.3"
[com.github.rayboot.svr/svr-logansquare "1.0.3"]

Dependencies

compile (4)

Group / Artifact Type Version
com.squareup.okhttp : okhttp-urlconnection jar 2.3.0
com.squareup.okhttp : okhttp jar 2.3.0
com.mcxiaoke.volley : library jar 1.0.17
com.bluelinelabs : logansquare jar 1.1.0

Project Modules

There are no modules declared in this project.

SimpleVolleyRequest

*A simple json request use volley. Svr use builder pattern to implement volley request.

What is Svr

The full name is simple volley request.

It's implement Volley request like builder pattern.

Svr auto set every request's tag and will auto cancel the request which set tag when the activity onDestory().

Features

        Svr.builder(this, BaseModule.class) //context must implement INetWork,it will auto to set the request tag.Or you can use .tag() function to set tag.
                .url()              //the url you will request.
                .method()           //default is post you could set get or post.
                .shouldCache(true)  //should cache
                .requestParams()    //set request parmas.
                .setHeaders()       //set request headers.
                .clickView()        //you could set some view it can't double click when the request is not return
                .stateView()        //set stateView to Svr.Svr will help to show some http state
                .timeout()          //set time out time.By default wifi is 15' others is 60'
                .finishListener()   //either fault or success request all will callback this function
                .cacheListener()    //return cache data
                .post2Queue();      //the last function to add the request to queue

So the simple request is like

        Svr.builder(this, BaseModule.class)
                .url("http://**************")
                .finishListener(new IFinishListener<BaseModule>() {
            @Override
            public void onFinishResponse(boolean success, BaseModule response, VolleyError error) {
                        if (success) {
                            //do something with response
                        } else {
                            //deal some error info
                        }
            }
        }).post2Queue();

How to use

###First of all You must init Svr in your application like

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SvrVolley.Init(this, new IJsonParser() {
            @Override
            public <T> T parseJson(String jsonString, Class<T> mClazz) {
                return new Gson().fromJson(jsonString, mClazz);
            }
        }, new OkHttpStack()); //if you don't use okhttp,please set null in the last param.
    }
}

###Second In your base activity you must implements INetWork.

INetWork have a method is getVolleyTag.This function get the tag to volley request.The tag will add to every request , and it cancel all the request with this tag when the activity destory.

Third in your base activity override onDestory() function,and call

        SvrVolley.getInstance().cancelPendingRequests(getVolleyTag());

So the BaseActivity like

public class BaseActivity extends Activity implements INetWork {
    protected String TAG = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = ((Object) this).getClass().getSimpleName();
    }

    @Override
    protected void onDestroy() {
        SvrVolley.getInstance().cancelPendingRequests(getVolleyTag());
        super.onDestroy();
    }

    @Override
    public String getVolleyTag() {
        return TAG +  this.hashCode();
    }
}

###The last step add some request in your activity

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StateView stateView = (StateView) findViewById(R.id.stateView);
        stateView.setOnRetryListener(new StateView.RetryListener() {
            @Override
            public void onRetry() {
                Toast.makeText(MainActivity.this, "test retry", Toast.LENGTH_SHORT).show();
            }
        });

        Svr.builder(this, BaseModule.class).stateView(stateView)
                .url("http://**********")
                .finishListener(new VolleyRequest.FinishListener<BaseModule>() {
            @Override
            public void onFinishResponse(boolean isSuccess, BaseModule response, VolleyError error) {


            }
        }).post2Queue();
    }

}

The StateView help you to show some error infomation like NO_NETWORK,ERROR_404 and so on.

Usage

Gradle:

dependencies {
    compile 'com.github.rayboot.svr:svr:2.2.0'
}

Versions

Version
1.0.3