From a1a9bc5bcaef7afbd992821e2363a46f12e1a340 Mon Sep 17 00:00:00 2001 From: Akash Kava Date: Fri, 22 Apr 2016 13:59:28 +0530 Subject: [PATCH] Upgraded to Retrofit2 and OkHttp3 --- .idea/gradle.xml | 10 ++- .idea/vcs.xml | 6 -- app/build.gradle | 3 +- app/src/main/AndroidManifest.xml | 3 + .../hypercubesample/AppService.java | 66 +++++++++++++++++++ .../hypercubesample/MainActivity.java | 48 ++++++++++++++ .../hypercubesample/MainApplication.java | 18 +++++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- hypercube/build.gradle | 10 +-- .../hypercube/service/Promise.java | 15 +++-- .../hypercube/service/RestService.java | 5 +- .../service/json/JacksonConverterFactory.java | 14 ++-- .../service/json/PromiseConverterFactory.java | 14 ++-- 14 files changed, 178 insertions(+), 40 deletions(-) delete mode 100644 .idea/vcs.xml create mode 100644 app/src/main/java/com/neurospeech/hypercubesample/AppService.java create mode 100644 app/src/main/java/com/neurospeech/hypercubesample/MainApplication.java diff --git a/.idea/gradle.xml b/.idea/gradle.xml index b664a38..24634f7 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -5,8 +5,7 @@ + diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 6564d52..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index e44ec4f..9df6ce2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,8 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' + compile project(':hypercube') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bcc5d85..1f5fe03 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,8 +2,11 @@ + + posts(){ + return api.posts(); + } + + public Promise put(Post post){ + return api.put(post.id,post); + } + + + + + public interface API{ + + @GET("/posts") + Promise posts(); + + + @PUT("/posts/{id}") + Promise put(@Path("id") int id, @Body Post post); + + } + + public static class Post{ + + @JsonProperty("id") + public int id; + + @JsonProperty("userId") + public int userId; + + @JsonProperty("title") + public String title; + + @JsonProperty("body") + public String body; + + } + +} diff --git a/app/src/main/java/com/neurospeech/hypercubesample/MainActivity.java b/app/src/main/java/com/neurospeech/hypercubesample/MainActivity.java index d02a950..ec60b32 100644 --- a/app/src/main/java/com/neurospeech/hypercubesample/MainActivity.java +++ b/app/src/main/java/com/neurospeech/hypercubesample/MainActivity.java @@ -2,6 +2,11 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.widget.Toast; + +import com.neurospeech.hypercube.HyperCubeApplication; +import com.neurospeech.hypercube.service.IResultListener; +import com.neurospeech.hypercube.service.Promise; public class MainActivity extends AppCompatActivity { @@ -9,5 +14,48 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + + HyperCubeApplication.current.post(new Runnable() { + @Override + public void run() { + runService(); + + } + },1000); + + } + + private void runService() { + + Toast.makeText(MainActivity.this,"Running Service",Toast.LENGTH_SHORT).show(); + + final AppService service = new AppService(this); + + service.posts().then(new IResultListener() { + @Override + public void onResult(Promise promise) { + if(promise.getError()!=null){ + Toast.makeText(MainActivity.this,promise.getError(),Toast.LENGTH_SHORT).show(); + }else{ + final AppService.Post first = promise.getResult()[0]; + first.body = "testing"; + service.put(first).then(new IResultListener() { + @Override + public void onResult(Promise promise) { + if(promise.getError()!=null) { + Toast.makeText(MainActivity.this, promise.getError(), Toast.LENGTH_SHORT).show(); + }else{ + if(first.body != promise.getResult().body){ + Toast.makeText(MainActivity.this, "Not Saved", Toast.LENGTH_SHORT).show(); + }else{ + Toast.makeText(MainActivity.this, "Saved Successfully", Toast.LENGTH_SHORT).show(); + } + } + } + }); + } + } + }); } } diff --git a/app/src/main/java/com/neurospeech/hypercubesample/MainApplication.java b/app/src/main/java/com/neurospeech/hypercubesample/MainApplication.java new file mode 100644 index 0000000..6985344 --- /dev/null +++ b/app/src/main/java/com/neurospeech/hypercubesample/MainApplication.java @@ -0,0 +1,18 @@ +package com.neurospeech.hypercubesample; + +import android.app.Application; + +import com.neurospeech.hypercube.HyperCubeApplication; + +/** + * Created by akash.kava on 22-04-2016. + */ +public class MainApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + + HyperCubeApplication.init(this); + } +} diff --git a/build.gradle b/build.gradle index e0b366a..168f129 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:1.5.0' + classpath 'com.android.tools.build:gradle:2.0.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index f23df6e..2e453d3 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Oct 21 11:34:03 PDT 2015 +#Fri Apr 22 13:18:08 IST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip diff --git a/hypercube/build.gradle b/hypercube/build.gradle index 2315f65..e4c50b0 100644 --- a/hypercube/build.gradle +++ b/hypercube/build.gradle @@ -25,8 +25,8 @@ android { defaultConfig { minSdkVersion 15 targetSdkVersion 23 - versionCode 4 - versionName "1.04" + versionCode 5 + versionName "1.05" } buildTypes { release { @@ -41,15 +41,15 @@ dependencies { testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' - compile 'com.squareup.okhttp:okhttp:2.5.0' - compile 'com.squareup.okhttp:okhttp-urlconnection:2.5.0' + compile 'com.squareup.okhttp3:okhttp:3.2.0' + compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0' //compile 'com.google.android.gms:play-services-gcm:7.8.0' //compile 'me.leolin:ShortcutBadger:1.1.3@aar' //Tablayout compile 'com.android.support:design:23.1.1' - compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' + compile 'com.squareup.retrofit2:retrofit:2.0.2' compile( [group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.4.1'], [group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.4.1'], diff --git a/hypercube/src/main/java/com/neurospeech/hypercube/service/Promise.java b/hypercube/src/main/java/com/neurospeech/hypercube/service/Promise.java index 8dd0cde..d3f666f 100644 --- a/hypercube/src/main/java/com/neurospeech/hypercube/service/Promise.java +++ b/hypercube/src/main/java/com/neurospeech/hypercube/service/Promise.java @@ -19,9 +19,10 @@ import java.util.ArrayList; import java.util.List; -import retrofit.Callback; -import retrofit.Response; -import retrofit.Retrofit; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; /** * Created by .kava on 10/20/2015. @@ -70,11 +71,11 @@ public Callback callback() { return new Callback() { - public void onResponse(final Response response, Retrofit retrofit) { - + @Override + public void onResponse(Call call, Response response) { - if (response.isSuccess()) { + if (response.isSuccessful()) { onResult(response.body(), null); } else { try { @@ -96,7 +97,7 @@ public void onResponse(final Response response, Retrofit retrofit) { } @Override - public void onFailure(Throwable t) { + public void onFailure(Call call, Throwable t) { String e = HyperCubeApplication.toString(t); //Log.e("Error Response:", e); HyperCubeApplication.current.logError(e); diff --git a/hypercube/src/main/java/com/neurospeech/hypercube/service/RestService.java b/hypercube/src/main/java/com/neurospeech/hypercube/service/RestService.java index 80ca2fb..16308ba 100644 --- a/hypercube/src/main/java/com/neurospeech/hypercube/service/RestService.java +++ b/hypercube/src/main/java/com/neurospeech/hypercube/service/RestService.java @@ -5,9 +5,10 @@ import com.neurospeech.hypercube.service.json.JacksonConverterFactory; import com.neurospeech.hypercube.service.json.PromiseConverterFactory; -import com.squareup.okhttp.OkHttpClient; -import retrofit.Retrofit; + +import okhttp3.OkHttpClient; +import retrofit2.Retrofit; /** * Created by Gigster on 30-12-2015. diff --git a/hypercube/src/main/java/com/neurospeech/hypercube/service/json/JacksonConverterFactory.java b/hypercube/src/main/java/com/neurospeech/hypercube/service/json/JacksonConverterFactory.java index 1df368a..1126844 100644 --- a/hypercube/src/main/java/com/neurospeech/hypercube/service/json/JacksonConverterFactory.java +++ b/hypercube/src/main/java/com/neurospeech/hypercube/service/json/JacksonConverterFactory.java @@ -5,17 +5,17 @@ import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.PropertyNamingStrategy; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.RequestBody; -import com.squareup.okhttp.ResponseBody; import java.io.IOException; import java.io.Reader; import java.lang.annotation.Annotation; import java.lang.reflect.Type; -import retrofit.Converter; -import retrofit.Retrofit; +import okhttp3.MediaType; +import okhttp3.RequestBody; +import okhttp3.ResponseBody; +import retrofit2.Converter; +import retrofit2.Retrofit; /** * A {@linkplain Converter.Factory converter} which uses Jackson. @@ -46,14 +46,14 @@ private JacksonConverterFactory(ObjectMapper mapper) { } @Override - public Converter fromResponseBody(Type type, Annotation[] annotations) { + public Converter responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { JavaType javaType = mapper.getTypeFactory().constructType(type); ObjectReader reader = mapper.reader(javaType); return new JacksonResponseBodyConverter<>(reader); } @Override - public Converter toRequestBody(Type type, Annotation[] annotations) { + public Converter requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { JavaType javaType = mapper.getTypeFactory().constructType(type); ObjectWriter writer = mapper.writerWithType(javaType); return new JacksonRequestBodyConverter<>(writer); diff --git a/hypercube/src/main/java/com/neurospeech/hypercube/service/json/PromiseConverterFactory.java b/hypercube/src/main/java/com/neurospeech/hypercube/service/json/PromiseConverterFactory.java index aefff93..4cbf5ad 100644 --- a/hypercube/src/main/java/com/neurospeech/hypercube/service/json/PromiseConverterFactory.java +++ b/hypercube/src/main/java/com/neurospeech/hypercube/service/json/PromiseConverterFactory.java @@ -9,17 +9,17 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import retrofit.Call; -import retrofit.CallAdapter; -import retrofit.Callback; -import retrofit.Converter; -import retrofit.Response; -import retrofit.Retrofit; +import retrofit2.Call; +import retrofit2.CallAdapter; +import retrofit2.Callback; +import retrofit2.Converter; +import retrofit2.Response; +import retrofit2.Retrofit; /** * Created by Akash on 12/5/2015. */ -public class PromiseConverterFactory implements CallAdapter.Factory { +public class PromiseConverterFactory extends CallAdapter.Factory { public static PromiseConverterFactory create(){ return new PromiseConverterFactory();