Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions JsonApiExample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.gradle
/local.properties
/.idea/workspace.xml
.DS_Store
.idea/
*.iml
build/
.gradletasknamecache

crashlytics.properties
crashlytics-build.properties
com_crashlytics_export_strings.xml
dagger-proguard-keepnames.cfg
42 changes: 42 additions & 0 deletions JsonApiExample/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
language: android
jdk: oraclejdk8
sudo: false
env:
- GRADLE_OPTS='-Dorg.gradle.jvmargs="-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError"'

machine:
environment:
GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError"'

android:
components:
- tools
- platform-tools
- build-tools-25.0.1
- android-25
- extra-android-m2repository
# Uncomment this if you need emulator
# - sys-img-x86-android-23
licenses:
- 'android-sdk-license-.+'

before_install:
- export TERM=dumb
- chmod +x gradlew

# Uncomment this if you need emulator
# before_script:
# - echo no | android create avd --force -n test -t android-21 --abi x86
# - emulator -avd test -no-skin -no-audio -no-window &
# - android-wait-for-emulator
# - adb shell input keyevent 82 &

script:
- ./gradlew checkstyle lintProductionRelease testProductionReleaseUnitTest

branches:
only:
- master
- production

# after_success: uncomment this and add your own deployment targets
87 changes: 87 additions & 0 deletions JsonApiExample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
JsonApi Android Example
=======================================

This example build on top of [retrofit2](https://github.com/square/retrofit) + [moshi](https://github.com/square/moshi) + [moshi-jsonapi](https://github.com/kamikat/moshi-jsonapi)

Simple ToDo Android application.

You can:
* Register
* Login
* See ToDo List
* Create new ToDo
* Update existing Todo

you can't logout :p

# How can I add my own requests?
### Parse answer
JsonApi dictates following answer format:

```
{
"data": {
"id": "5",
"type": "todo-items",
"links": {
"self": "http://localhost:3000/v1/todo-items/5"
},
"attributes": {
"title": "Meggings 8-bit irony tousled mlkshk neutra crucifix lumbersexual kogi.",
"text": "Meh wolf whatever salvia cardigan seitan paleo. Health poutine listicle goth scenester kitsch fanny pack. Narwhal cold-pressed hashtag goth umami.",
}
}
}
```

Good news, you don't have to parse such a _strange_ json by yourself. Just create following object:
```
@JsonApi(type = "todo-items")
public class ToDo extends moe.banana.jsonapi2.Resource {
String title;
String text;

public ToDo(String title, String text) {
this.title = title;
this.text = text;
}
}
```
First of all your object should extends from `Resource` class.
As you can see `ToDo`'s object fields coincide with json's keys under `attributes` fields.
If you want object field name and json key name to differ then use `@Json(name="first-name") String name` annotation.
Also `type` in json and type inside `@JsonApi(type = )` annotation of `ToDo` class should be the same.

After than just wrap your class by `ObjectDocument<ToDo>` in case of single object or `ArrayDocument<ToDo>` in case of array to parse answer.

So your Retrofit will look like:

```
@GET("todo-items/") Observable<moe.banana.jsonapi2.ArrayDocument<ToDo>> todos();
```


### Prepare request

When you do POST/PUT/PATCH then probably you need to send some info in request body. JsonApi dictates following structure:
```
{
"data": {
"type": "todo-items",
"attributes": {
"title": "Nice title here",
"text": "Text you never seen before ;)"
}
}
}
```
which is the same format as above for answers. So create object following rules above.

Now, your retrofit request will look like:
```
@POST("todo-items/") Observable<ResponseBody> createToDo(@Body moe.banana.jsonapi2.Document<ToDo> todo);
```


[<img src="http://www.flatstack.com/logo.svg" width="100"/>](http://www.flatstack.com)

100 changes: 100 additions & 0 deletions JsonApiExample/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply from: 'checkstyle/checkstyle.gradle'

apply from: '../deps.gradle'

ext {
APPLICATION_ID = "com.flatstack.android"
isCI = "true".equals(System.getenv("CI"))
commitMessage = 'git log -1 --pretty=%B'.execute().text.trim()
}

android {
compileSdkVersion versions.TARGET_SDK_VERSION
buildToolsVersion versions.BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion versions.MIN_SDK_VERSION
targetSdkVersion versions.TARGET_SDK_VERSION

applicationId APPLICATION_ID
versionCode 1
versionName '1.0-beta'
}

productFlavors {
staging {
buildConfigField "String", "API_URL", "\"https://example-staging.com\""
applicationIdSuffix ".staging"
}

production {
buildConfigField "String", "API_URL", "\"https://example.com\""
}
}

buildTypes {
debug {
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

dexOptions {
preDexLibraries = !isCI
}

packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/services/javax.annotation.processing.Processor'
}

lintOptions {
textReport true
textOutput "stdout"
lintConfig file("$projectDir/lint.xml")
warningsAsErrors true
}
}

repositories {
mavenCentral()
maven { url "https://jitpack.io" }
}

dependencies {
compile supportLibs
compile rxJavaLibs
compile retrofitLibs
compile okHttpLibs

compile 'com.jakewharton:butterknife:7.0.0' // view injection
compile 'com.squareup.moshi:moshi:1.4.0'
compile 'moe.banana:moshi-jsonapi:3.2.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.squareup.retrofit2:converter-moshi:2.2.0'
compile 'com.github.GrenderG:Toasty:1.1.4'

compile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
compile 'com.facebook.stetho:stetho:1.4.2'

testCompile unitTestLibs

compile 'com.google.code.gson:gson:2.4'
}

configurations {
testCompile.exclude module: 'commons-logging'
testCompile.exclude module: 'httpclient'
}

apply from: "quality.gradle"
17 changes: 17 additions & 0 deletions JsonApiExample/app/checkstyle/checkstyle.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: 'checkstyle'

check.dependsOn 'checkstyle'

checkstyle {
toolVersion '6.5'

configFile file("checkstyle/checkstyle.xml")
configProperties.checkstyleSuppressionFilterPath = file("checkstyle/suppressions.xml")
.absolutePath
}
task checkstyle(type: Checkstyle, group: 'verification') {
source 'src'
include '**/*.java'
exclude '**/gen/**'
classpath = files()
}
Loading