Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 57afa93

Browse files
author
Langston Smith
authored
Adding null checks in OptimizationActivity response (#1184)
1 parent 275a628 commit 57afa93

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/OptimizationActivity.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import android.graphics.BitmapFactory;
44
import android.graphics.Color;
55
import android.os.Bundle;
6-
import androidx.annotation.NonNull;
7-
import androidx.appcompat.app.AppCompatActivity;
86
import android.widget.Toast;
97

108
import com.mapbox.api.directions.v5.DirectionsCriteria;
@@ -29,6 +27,8 @@
2927
import java.util.ArrayList;
3028
import java.util.List;
3129

30+
import androidx.annotation.NonNull;
31+
import androidx.appcompat.app.AppCompatActivity;
3232
import retrofit2.Call;
3333
import retrofit2.Callback;
3434
import retrofit2.Response;
@@ -113,7 +113,7 @@ private void initMarkerIconSymbolLayer(@NonNull Style loadedMapStyle) {
113113
iconSize(1f),
114114
iconAllowOverlap(true),
115115
iconIgnorePlacement(true),
116-
iconOffset(new Float[] {0f, -4f})
116+
iconOffset(new Float[] {0f, -7f})
117117
));
118118
}
119119

@@ -212,22 +212,32 @@ private void getOptimizedRoute(@NonNull final Style style, List<Point> coordinat
212212
@Override
213213
public void onResponse(Call<OptimizationResponse> call, Response<OptimizationResponse> response) {
214214
if (!response.isSuccessful()) {
215-
Timber.d( getString(R.string.no_success));
215+
Timber.d(getString(R.string.no_success));
216216
Toast.makeText(OptimizationActivity.this, R.string.no_success, Toast.LENGTH_SHORT).show();
217-
return;
218217
} else {
219-
if (response.body().trips().isEmpty()) {
220-
Timber.d("%s size = %s", getString(R.string.successful_but_no_routes), response.body().trips().size());
221-
222-
Toast.makeText(OptimizationActivity.this, R.string.successful_but_no_routes,
223-
Toast.LENGTH_SHORT).show();
224-
return;
218+
if (response.body() != null) {
219+
List<DirectionsRoute> routes = response.body().trips();
220+
if (routes != null) {
221+
if (routes.isEmpty()) {
222+
Timber.d("%s size = %s", getString(R.string.successful_but_no_routes), routes.size());
223+
Toast.makeText(OptimizationActivity.this, R.string.successful_but_no_routes,
224+
Toast.LENGTH_SHORT).show();
225+
} else {
226+
// Get most optimized route from API response
227+
optimizedRoute = routes.get(0);
228+
drawOptimizedRoute(style, optimizedRoute);
229+
}
230+
} else {
231+
Timber.d("list of routes in the response is null");
232+
Toast.makeText(OptimizationActivity.this, String.format(getString(R.string.null_in_response),
233+
"The Optimization API response's list of routes"), Toast.LENGTH_SHORT).show();
234+
}
235+
} else {
236+
Timber.d("response.body() is null");
237+
Toast.makeText(OptimizationActivity.this, String.format(getString(R.string.null_in_response),
238+
"The Optimization API response's body"), Toast.LENGTH_SHORT).show();
225239
}
226240
}
227-
228-
// Get most optimized route from API response
229-
optimizedRoute = response.body().trips().get(0);
230-
drawOptimizedRoute(style, optimizedRoute);
231241
}
232242

233243
@Override

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
<string name="only_twelve_stops_allowed">Only 12 stops allowed on route. Press on the map and hold to clear markers.</string>
211211
<string name="click_instructions">Tap on the map to select a maximum of 12 locations</string>
212212
<string name="successful_but_no_routes">No routes found for that location</string>
213+
<string name="null_in_response">%1$s is null.</string>
213214
<string name="no_success">No routes found, make sure you set the right user and access token</string>
214215

215216
<!-- Rotation extrusion -->

0 commit comments

Comments
 (0)