Skip to content
This repository has been archived by the owner on Nov 26, 2022. It is now read-only.
/ httprequest Public archive

okhttp3 wrapper with sqlite caching, and chuck logingok

License

Notifications You must be signed in to change notification settings

deckyfx/httprequest

Repository files navigation

httprequest

Extension of okhttp3, with many short hand methods, and event listeners

  • okhttp3
  • deckyfx/dbhelper
  • deckyfx/simpleadapter
  • greendao

Sample CodUsage

Add it in your root build.gradle at the end of repositories:

allprojects {
	repositories {
	...
		maven { url 'https://jitpack.io' }
	}
}

Add the dependency

dependencies {
    compile 'com.github.deckyfx.httprequest:-SNAPSHOT'
    compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.9.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'nl.qbusict:cupboard:2.2.0'
    compile "com.android.support:design:25.3.1"
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile "com.android.support:support-v4:+"
}

Sample Code

Init the class and configure it

...
HTTPRequest HTTP_CLIENT = new HTTPRequest(App.MAIN_CONTEXT);
HTTP_CLIENT.setRequestTimeOut(15);
HTTP_CLIENT.initRequestCache();
HTTP_CLIENT.initCookieStore();
HTTP_CLIENT.setBaseURL(/* your REST API home url */);
HTTP_CLIENT.addApplicationInterceptor(/* Interceptor goes here */);
HTTP_CLIENT.enableHTTPLogging(/* HttpLoggingInterceptor.Level */);
HTTP_CLIENT.enableChuckLogging(/* Should we show notification? */);
HTTP_CLIENT.initHTTPCLient();
...

Then Start Request by Calling

...
Request request = new Request.Builder(this)
            .url("http://google.com")
            .build();
this.G.HTTP_CLIENT.send(request);
...

POST data

...
Request request = new Request.Builder(this)
            .path("/apipath")
            .addParam("name", "John Doe")
            .method(HttpMethod.POST)
            .build();
this.G.HTTP_CLIENT.send(request);
...

Upload File

...
Request request = new Request.Builder(this)
           .path("/apipath")
           .addParam("file", new File(/* File Path*/))
           .method(HttpMethod.POST)
           .build();
this.G.HTTP_CLIENT.send(request);
...

Header and Listener

...
Request request = new Request.Builder(this)
           .path("/apipath")
           .addHeader("XHTTPHeader", "value")
           .addParam("asGET", "value")
           .method(HttpMethod.GET)
           .listener(this)
           .build();
this.G.HTTP_CLIENT.send(request);
...

More sample is [here]

Feature: