Skip to content

Commit d16d5cf

Browse files
fix: Add logging in case of http error and add user agent to all requests
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 432d5f0 commit d16d5cf

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

app/src/main/java/fr/dudie/nominatim/client/TalkJsonNominatimClient.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,16 @@ public List<Address> search(final NominatimSearchRequest search) throws IOExcept
153153
.build();
154154

155155
Response response = httpClient.newCall(requesthttp).execute();
156+
ResponseBody responseBody = response.body();
156157
if (response.isSuccessful()) {
157-
ResponseBody responseBody = response.body();
158158
if (responseBody != null) {
159159
return gson.fromJson(responseBody.string(), new TypeToken<List<Address>>() {
160160
}.getType());
161161
}
162162
} else {
163+
if (responseBody != null) {
164+
Log.w(TAG, "search failed: " + response.code() + " " + responseBody);
165+
}
163166
Log.w(TAG, "search failed with HTTP-code: " + response.code());
164167
}
165168

@@ -178,16 +181,22 @@ public Address getAddress(final NominatimReverseRequest reverse) throws IOExcept
178181
Log.d(TAG, "reverse geocoding url: " + apiCall);
179182

180183
Request requesthttp = new Request.Builder()
181-
.addHeader("accept", "application/json")
182-
.url(apiCall)
183-
.build();
184+
.addHeader("accept", "application/json")
185+
.addHeader("User-Agent", ApiUtils.getUserAgent())
186+
.url(apiCall)
187+
.build();
184188

185189
Response response = httpClient.newCall(requesthttp).execute();
190+
ResponseBody responseBody = response.body();
186191
if (response.isSuccessful()) {
187-
ResponseBody responseBody = response.body();
188192
if (responseBody != null) {
189193
return gson.fromJson(responseBody.string(), Address.class);
190194
}
195+
} else {
196+
if (responseBody != null) {
197+
Log.w(TAG, "get address failed: " + response.code() + " " + responseBody);
198+
}
199+
Log.w(TAG, "get address failed with HTTP-code: " + response.code());
191200
}
192201

193202
return null;
@@ -203,18 +212,25 @@ public List<Address> lookupAddress(final NominatimLookupRequest lookup) throws I
203212

204213
final String apiCall = String.format("%s&%s", lookupUrl, lookup.getQueryString());
205214
Log.d(TAG, "lookup url: " + apiCall);
215+
206216
Request requesthttp = new Request.Builder()
207217
.addHeader("accept", "application/json")
218+
.addHeader("User-Agent", ApiUtils.getUserAgent())
208219
.url(apiCall)
209220
.build();
210221

211222
Response response = httpClient.newCall(requesthttp).execute();
223+
ResponseBody responseBody = response.body();
212224
if (response.isSuccessful()) {
213-
ResponseBody responseBody = response.body();
214225
if (responseBody != null) {
215226
return gson.fromJson(responseBody.string(), new TypeToken<List<Address>>() {
216227
}.getType());
217228
}
229+
} else {
230+
if (responseBody != null) {
231+
Log.w(TAG, "address lookup failed: " + response.code() + " " + responseBody);
232+
}
233+
Log.w(TAG, "address lookup failed with HTTP-code: " + response.code());
218234
}
219235

220236
return new ArrayList<>();

0 commit comments

Comments
 (0)