Skip to content

Commit 9f6e936

Browse files
support json options
1 parent bf025d4 commit 9f6e936

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
compile 'com.squareup.okhttp3:okhttp:3.12.0'
1919
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
2020
implementation 'com.google.guava:guava:28.1-jre'
21+
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.5'
2122

2223
testCompile group: 'junit', name: 'junit', version: '4.12'
2324
}

src/main/java/com/osrm/client/DistanceService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
import java.util.List;
44

55
public interface DistanceService {
6-
DistanceMatrix buildDistanceMatrix(List<GeoLocation> coordinates, double speedRate, String country, String token, String profile, String startTime);
6+
DistanceMatrix buildDistanceMatrix(List<GeoLocation> coordinates, double speedRate, String country, String token, String profile, String options);
77
}

src/main/java/com/osrm/client/OSRMClient.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.osrm.client;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import java.io.UnsupportedEncodingException;
45
import java.net.URLEncoder;
56
import java.nio.charset.StandardCharsets;
67
import java.util.ArrayList;
78
import java.util.List;
9+
import java.util.Map;
810
import java.util.concurrent.TimeUnit;
911
import okhttp3.MediaType;
1012
import okhttp3.OkHttpClient;
@@ -28,7 +30,7 @@ public OSRMClient(String uri) throws EmptyUrlException {
2830

2931
public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, double speedRate, String country,
3032
String token, String profile,
31-
String startTime) throws OptimizationDistanceMatrixException {
33+
String options) throws OptimizationDistanceMatrixException {
3234
Builder requestBuilder = new Builder();
3335

3436
requestBuilder.readTimeout(900000, TimeUnit.MILLISECONDS);
@@ -49,7 +51,7 @@ public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, doubl
4951

5052
paramsString += "&speedRate=" + speedRate;
5153
paramsString += "&country=" + country;
52-
paramsString += encodeStartTime(startTime);
54+
paramsString += encodeJsonToUrlParams(options);
5355

5456
RequestBody body = RequestBody.create(mediaType, "loc=" + paramsString);
5557

@@ -79,18 +81,21 @@ public OSRMDistanceResponse getDistanceMatrix(List<GeoLocation> locations, doubl
7981
throw new DistanceMatrixResponseException("OSRM Error: " + response);
8082
}
8183

82-
private String encodeStartTime(String startTime) {
83-
String paramsString = "";
84-
if (startTime == null || startTime.isEmpty()) {
85-
return paramsString;
86-
}
84+
public static String encodeJsonToUrlParams(String options) {
85+
ObjectMapper objectMapper = new ObjectMapper();
86+
StringBuilder urlParams = new StringBuilder();
8787
try {
88-
String encodedStartTime = URLEncoder.encode(startTime, StandardCharsets.UTF_8.toString());
89-
paramsString += "&start_time=" + encodedStartTime;
90-
} catch (Exception e){
91-
throw new OptimizationDistanceMatrixException("Error while encoding startTime parameter");
88+
Map<String, Object> map = objectMapper.readValue(options, Map.class);
89+
90+
for (Map.Entry<String, Object> entry : map.entrySet()) {
91+
String key = URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.toString());
92+
String value = URLEncoder.encode(String.valueOf(entry.getValue()), StandardCharsets.UTF_8.toString());
93+
urlParams.append("&").append(key).append("=").append(value);
94+
}
95+
}catch (Exception e) {
96+
System.out.print("Error getUnsuccessfulResponse.fromJSON: " + e.getMessage());
9297
}
93-
return paramsString;
98+
return urlParams.toString();
9499
}
95100

96101
private UnsuccessfulResponse getUnsuccessfulResponse(Response response){

0 commit comments

Comments
 (0)