Hi,
I am trying to call the OpenRouteService Directions API from Android Studio, however I haven´t been able to get a response. Here is how I did it:
In OnCreate:
JSONObject postData = new JSONObject();
JSONArray coordenadas = new JSONArray();
try {
coordenadas.put(new JSONArray().put(8.681495).put(49.41461));
coordenadas.put(new JSONArray().put(8.687872).put(49.420318));
postData.put("coordinates", coordenadas);
new SendDeviceDetails().execute("https://api.openrouteservice.org/v2/directions/driving-car/geojson", postData.toString());
} catch (JSONException e) {
e.printStackTrace();
}
and SendDeviceDetails AsyncTask is:
private class SendDeviceDetails extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();
httpURLConnection.setRequestProperty("Authorization", MY_API_KEY);
httpURLConnection.setRequestProperty("Accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData=" + params[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(inputStreamReader);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("TAG", result); // this is expecting a response code to be sent from your server upon receiving the POST data
}
}
This is what I am getting in run:
W/System.err: java.io.FileNotFoundException: https://api.openrouteservice.org/v2/directions/driving-car/geojson
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255)
And this what I get in LogCat:
E/ResolverController: No valid NAT64 prefix (101, <unspecified>/0)
Does anyone know how to solve it?
Thanks!!