From 35af6aa70e2372d4e9c5dcb1f4a40b60389da149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Sun, 30 Sep 2018 18:58:30 +0200 Subject: [PATCH] Rewrite AsyncTask to rxjava-retrofit approach --- .../uam/wmi/findmytutor/model/Coordinate.java | 20 +-- .../service/CoordinateService.java | 14 +- .../utils/BroadcastLocalizationHandler.java | 121 ++++-------------- 3 files changed, 48 insertions(+), 107 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java b/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java index 8a7fcfb..cc98df8 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java @@ -19,10 +19,10 @@ public class Coordinate extends BaseResponse { private Double latitude; @SerializedName("longitude") - private Float longitude = null; + private Double longitude = null; @SerializedName("altitude") - private Float altitude = null; + private Double altitude = null; @SerializedName("userId") private String userId = null; @@ -39,13 +39,13 @@ public class Coordinate extends BaseResponse { @SerializedName("label") private String label; - /* public Coordinate (Float latitude, Float longitude, String userId, String label ) { + public Coordinate (Double latitude, Double longitude, String label, String userId) { this.latitude = latitude; this.longitude = longitude; this.userId = userId; this.label = label; - }*/ + } public Coordinate (Double latitude) { this.latitude = latitude; @@ -87,7 +87,7 @@ public class Coordinate extends BaseResponse { this.latitude = latitude; } - public Coordinate longitude(Float longitude) { + public Coordinate longitude(Double longitude) { this.longitude = longitude; return this; } @@ -97,15 +97,15 @@ public class Coordinate extends BaseResponse { * @return longitude **/ @ApiModelProperty(required = true, value = "") - public Float getLongitude() { + public Double getLongitude() { return longitude; } - public void setLongitude(Float longitude) { + public void setLongitude(Double longitude) { this.longitude = longitude; } - public Coordinate altitude(Float altitude) { + public Coordinate altitude(Double altitude) { this.altitude = altitude; return this; } @@ -115,11 +115,11 @@ public class Coordinate extends BaseResponse { * @return altitude **/ @ApiModelProperty(required = true, value = "") - public Float getAltitude() { + public Double getAltitude() { return altitude; } - public void setAltitude(Float altitude) { + public void setAltitude(Double altitude) { this.altitude = altitude; } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java index cd85a2c..ea0b31b 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java @@ -1,6 +1,8 @@ package com.uam.wmi.findmytutor.service; import com.uam.wmi.findmytutor.model.Coordinate; +import com.uam.wmi.findmytutor.model.JwtToken; +import com.uam.wmi.findmytutor.model.LdapUser; import java.util.List; @@ -39,14 +41,18 @@ public interface CoordinateService { Call> getOnlineCoordinates(); /* - // Create note - @FormUrlEncoded - @POST("api/coordinates") + //works + @POST("api/coordinates") + Call postCoordinate(@Body RequestBody Coordinate); */ + + //works @POST("api/coordinates") - Call postCoordinate(@Body RequestBody Coordinate); + Single postCoordinate(@Body Coordinate coordinate); + + /* // Create note @FormUrlEncoded diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java index 63fc2be..51d5d7a 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java @@ -1,5 +1,6 @@ package com.uam.wmi.findmytutor.utils; +import android.annotation.SuppressLint; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -8,12 +9,18 @@ import android.os.AsyncTask; import android.util.ArrayMap; import android.util.Log; +import com.auth0.android.jwt.Claim; +import com.auth0.android.jwt.JWT; import com.uam.wmi.findmytutor.model.Coordinate; +import com.uam.wmi.findmytutor.model.JwtToken; +import com.uam.wmi.findmytutor.model.LdapUser; import com.uam.wmi.findmytutor.network.ApiClient; import com.uam.wmi.findmytutor.service.CoordinateService; import com.uam.wmi.findmytutor.service.LdapService; import org.json.JSONObject; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; import java.util.Map; @@ -31,53 +38,24 @@ import static android.content.ContentValues.TAG; import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; public class BroadcastLocalizationHandler extends BroadcastReceiver { - public static String str_receiver = "servicetutorial.service.receiver"; - - private IntentFilter filter = - new IntentFilter(str_receiver); - - CompositeDisposable disposable = new CompositeDisposable(); - CoordinateService coordinateService = ApiClient.getClient(getApplicationContext()) - .create(CoordinateService.class); - @Override public void onReceive(Context arg0, Intent intent) { Double latitude, longitude; latitude = intent.getDoubleExtra("latitude", 0); - Coordinate coordinate = new Coordinate(latitude); - - /* disposable.add(coordinateService.postCoordinate(coordinate) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeWith(new DisposableSingleObserver() { - @Override - public void onSuccess(Coordinate coordinate) { - Log.e("Service", "onSuccess: " + coordinate); - } - - @Override - public void onError(Throwable e) { - Log.e("Service", "onError: " + e.getMessage()); - } - }));*/ - - - // longitude = intent.getDoubleExtra("longitude",0); - Log.e("Background service", String.valueOf(latitude)); final PendingResult pendingResult = goAsync(); Task asyncTask = new Task(pendingResult, intent); asyncTask.execute(); - } private static class Task extends AsyncTask { private final PendingResult pendingResult; private final Intent intent; + private Double latitude; private CompositeDisposable disposable = new CompositeDisposable(); private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext()) .create(CoordinateService .class); @@ -85,78 +63,35 @@ public class BroadcastLocalizationHandler extends BroadcastReceiver { private Task(PendingResult pendingResult, Intent intent) { this.pendingResult = pendingResult; this.intent = intent; + this.latitude = intent.getDoubleExtra("latitude", 0); } @Override protected Object doInBackground(Object[] objects) { - //You should find out what causes this fancy/not reproducible error + //Here you can obtain value from receiver + Log.e("task", String.valueOf(this.latitude)); - /* Coordinate coordinate = new Coordinate(52.0); + Coordinate coordinate = new Coordinate(52.467099,16.927560, "android",PrefUtils.getUserId(getApplicationContext())); - disposable.add(coordinateService.postCoordinate(52.0) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribeWith(new DisposableObserver() { - *//* @Override - public void onSuccess(Coordinate coordinate) { - Log.e("Service", "onSuccess: " + coordinate); + disposable.add( + coordinateService + .postCoordinate(coordinate) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribeWith(new DisposableSingleObserver() { + @SuppressLint("LongLogTag") + @Override + public void onSuccess(Coordinate coord) { + Log.e("CoordinateService onSuccess", String.valueOf(coord)); - onPostExecute(true); - } -*//* - @Override - public void onNext(Coordinate coordinate) { - Log.e("Service", "onSuccess: " + coordinate); - } + } - @Override - public void onError(Throwable e) { - Log.e("Service", "onError: " + e.getMessage()); - } - - @Override - public void onComplete() { - - } - })); - - return null;*/ - - - Map jsonParams = new ArrayMap<>(); -//put something inside the map, could be null - jsonParams.put("latitude", 52.0); - jsonParams.put("longitude", 16.0); - jsonParams.put("userId", PrefUtils.getUserId(getApplicationContext())); - - RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),(new JSONObject(jsonParams)).toString()); -//serviceCaller is the interface initialized with retrofit.create... - Call response = coordinateService.postCoordinate(body); - - response.enqueue(new Callback() - { - @Override - public void onResponse(Call call, retrofit2.Response rawResponse) - { - try - { - //get your response.... - Log.d(TAG, "RetroFit2.0 :RetroGetLogin: " + rawResponse.body().string()); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - - @Override - public void onFailure(Call call, Throwable throwable) - { - // other stuff... - Log.d(TAG, "RetroFit2.0 :RetroGet error: "); - } - }); + @Override + public void onError(Throwable e) { + Log.e("LoginError", "onError: " + e.getMessage()); + } + })); return null; }