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; private Double latitude;
@SerializedName("longitude") @SerializedName("longitude")
private Float longitude = null; private Double longitude = null;
@SerializedName("altitude") @SerializedName("altitude")
private Float altitude = null; private Double altitude = null;
@SerializedName("userId") @SerializedName("userId")
private String userId = null; private String userId = null;
@ -39,13 +39,13 @@ public class Coordinate extends BaseResponse {
@SerializedName("label") @SerializedName("label")
private String 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.latitude = latitude;
this.longitude = longitude; this.longitude = longitude;
this.userId = userId; this.userId = userId;
this.label = label; this.label = label;
}*/ }
public Coordinate (Double latitude) { public Coordinate (Double latitude) {
this.latitude = latitude; this.latitude = latitude;
@ -87,7 +87,7 @@ public class Coordinate extends BaseResponse {
this.latitude = latitude; this.latitude = latitude;
} }
public Coordinate longitude(Float longitude) { public Coordinate longitude(Double longitude) {
this.longitude = longitude; this.longitude = longitude;
return this; return this;
} }
@ -97,15 +97,15 @@ public class Coordinate extends BaseResponse {
* @return longitude * @return longitude
**/ **/
@ApiModelProperty(required = true, value = "") @ApiModelProperty(required = true, value = "")
public Float getLongitude() { public Double getLongitude() {
return longitude; return longitude;
} }
public void setLongitude(Float longitude) { public void setLongitude(Double longitude) {
this.longitude = longitude; this.longitude = longitude;
} }
public Coordinate altitude(Float altitude) { public Coordinate altitude(Double altitude) {
this.altitude = altitude; this.altitude = altitude;
return this; return this;
} }
@ -115,11 +115,11 @@ public class Coordinate extends BaseResponse {
* @return altitude * @return altitude
**/ **/
@ApiModelProperty(required = true, value = "") @ApiModelProperty(required = true, value = "")
public Float getAltitude() { public Double getAltitude() {
return altitude; return altitude;
} }
public void setAltitude(Float altitude) { public void setAltitude(Double altitude) {
this.altitude = altitude; this.altitude = altitude;
} }

View File

@ -1,6 +1,8 @@
package com.uam.wmi.findmytutor.service; package com.uam.wmi.findmytutor.service;
import com.uam.wmi.findmytutor.model.Coordinate; 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; import java.util.List;
@ -39,14 +41,18 @@ public interface CoordinateService {
Call<List<Coordinate>> getOnlineCoordinates(); Call<List<Coordinate>> getOnlineCoordinates();
/* /*
// Create note
@FormUrlEncoded
@POST("api/coordinates")
//works
@POST("api/coordinates")
Call <ResponseBody> postCoordinate(@Body RequestBody Coordinate);
*/ */
//works
@POST("api/coordinates") @POST("api/coordinates")
Call<ResponseBody> postCoordinate(@Body RequestBody Coordinate); Single <Coordinate> postCoordinate(@Body Coordinate coordinate);
/* /*
// Create note // Create note
@FormUrlEncoded @FormUrlEncoded

View File

@ -1,5 +1,6 @@
package com.uam.wmi.findmytutor.utils; package com.uam.wmi.findmytutor.utils;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -8,12 +9,18 @@ import android.os.AsyncTask;
import android.util.ArrayMap; import android.util.ArrayMap;
import android.util.Log; 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.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.network.ApiClient;
import com.uam.wmi.findmytutor.service.CoordinateService; import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.service.LdapService; import com.uam.wmi.findmytutor.service.LdapService;
import org.json.JSONObject; import org.json.JSONObject;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import java.util.Map; import java.util.Map;
@ -31,53 +38,24 @@ import static android.content.ContentValues.TAG;
import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext; import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class BroadcastLocalizationHandler extends BroadcastReceiver { 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 @Override
public void onReceive(Context arg0, Intent intent) { public void onReceive(Context arg0, Intent intent) {
Double latitude, longitude; Double latitude, longitude;
latitude = intent.getDoubleExtra("latitude", 0); 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)); Log.e("Background service", String.valueOf(latitude));
final PendingResult pendingResult = goAsync(); final PendingResult pendingResult = goAsync();
Task asyncTask = new Task(pendingResult, intent); Task asyncTask = new Task(pendingResult, intent);
asyncTask.execute(); asyncTask.execute();
} }
private static class Task extends AsyncTask { private static class Task extends AsyncTask {
private final PendingResult pendingResult; private final PendingResult pendingResult;
private final Intent intent; private final Intent intent;
private Double latitude;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext()) private CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService .class); .create(CoordinateService .class);
@ -85,78 +63,35 @@ public class BroadcastLocalizationHandler extends BroadcastReceiver {
private Task(PendingResult pendingResult, Intent intent) { private Task(PendingResult pendingResult, Intent intent) {
this.pendingResult = pendingResult; this.pendingResult = pendingResult;
this.intent = intent; this.intent = intent;
this.latitude = intent.getDoubleExtra("latitude", 0);
} }
@Override @Override
protected Object doInBackground(Object[] objects) { 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) disposable.add(
.subscribeOn(Schedulers.io()) coordinateService
.observeOn(AndroidSchedulers.mainThread()) .postCoordinate(coordinate)
.subscribeWith(new DisposableObserver<Coordinate>() { .subscribeOn(Schedulers.io())
*//* @Override .observeOn(AndroidSchedulers.mainThread())
public void onSuccess(Coordinate coordinate) { .subscribeWith(new DisposableSingleObserver<Coordinate>() {
Log.e("Service", "onSuccess: " + 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 @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Log.e("Service", "onError: " + e.getMessage()); Log.e("LoginError", "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: ");
}
});
return null; return null;
} }