Skip to content

Commit

Permalink
Upgraded to Retrofit2 and OkHttp3
Browse files Browse the repository at this point in the history
  • Loading branch information
Akash Kava authored and Akash Kava committed Apr 22, 2016
1 parent cd213a9 commit a1a9bc5
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 40 deletions.
10 changes: 8 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.neurospeech.hypercubesample">

<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:name=".MainApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
Expand Down
66 changes: 66 additions & 0 deletions app/src/main/java/com/neurospeech/hypercubesample/AppService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.neurospeech.hypercubesample;

import android.content.Context;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.neurospeech.hypercube.service.Promise;

import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.PUT;
import retrofit2.http.Path;

/**
* Created by akash.kava on 22-04-2016.
*/
public class AppService extends com.neurospeech.hypercube.service.RestService {

API api;

protected AppService(Context context) {
super(context);


api = createAPIClient("http://jsonplaceholder.typicode.com",API.class);
}


public Promise<Post[]> posts(){
return api.posts();
}

public Promise<Post> put(Post post){
return api.put(post.id,post);
}




public interface API{

@GET("/posts")
Promise<Post[]> posts();


@PUT("/posts/{id}")
Promise<Post> 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;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,60 @@

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 {

@Override
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<AppService.Post[]>() {
@Override
public void onResult(Promise<AppService.Post[]> 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<AppService.Post>() {
@Override
public void onResult(Promise<AppService.Post> 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();
}
}
}
});
}
}
});
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions hypercube/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 4
versionName "1.04"
versionCode 5
versionName "1.05"
}
buildTypes {
release {
Expand All @@ -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'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -70,11 +71,11 @@ public Callback<T> callback() {


return new Callback<T>() {
public void onResponse(final Response<T> response, Retrofit retrofit) {


@Override
public void onResponse(Call<T> call, Response<T> response) {

if (response.isSuccess()) {
if (response.isSuccessful()) {
onResult(response.body(), null);
} else {
try {
Expand All @@ -96,7 +97,7 @@ public void onResponse(final Response<T> response, Retrofit retrofit) {
}

@Override
public void onFailure(Throwable t) {
public void onFailure(Call<T> call, Throwable t) {
String e = HyperCubeApplication.toString(t);
//Log.e("Error Response:", e);
HyperCubeApplication.current.logError(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -46,14 +46,14 @@ private JacksonConverterFactory(ObjectMapper mapper) {
}

@Override
public Converter<ResponseBody, ?> fromResponseBody(Type type, Annotation[] annotations) {
public Converter<okhttp3.ResponseBody, ?> 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<?, RequestBody> toRequestBody(Type type, Annotation[] annotations) {
public Converter<?, okhttp3.RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
JavaType javaType = mapper.getTypeFactory().constructType(type);
ObjectWriter writer = mapper.writerWithType(javaType);
return new JacksonRequestBodyConverter<>(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit a1a9bc5

Please sign in to comment.