Rewrite AsyncTask to rxjava-retrofit approach

This commit is contained in:
Mieszko Wrzeszczyński 2018-09-30 18:58:30 +02:00
parent 5d7022a32c
commit 35af6aa70e
3 changed files with 48 additions and 107 deletions

View File

@ -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;
}

View File

@ -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<List<Coordinate>> getOnlineCoordinates();
/*
// Create note
@FormUrlEncoded
@POST("api/coordinates")
//works
@POST("api/coordinates")
Call <ResponseBody> postCoordinate(@Body RequestBody Coordinate);
*/
//works
@POST("api/coordinates")
Call<ResponseBody> postCoordinate(@Body RequestBody Coordinate);
Single <Coordinate> postCoordinate(@Body Coordinate coordinate);
/*
// Create note
@FormUrlEncoded

View File

@ -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<Coordinate>() {
@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<Coordinate>() {
*//* @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<Coordinate>() {
@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<String, Object> 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<ResponseBody> response = coordinateService.postCoordinate(body);
response.enqueue(new Callback<ResponseBody>()
{
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse)
{
try
{
//get your response....
Log.d(TAG, "RetroFit2.0 :RetroGetLogin: " + rawResponse.body().string());
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> 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;
}