Я хочу отправить данные IoT из приложения Android в службу IoT SAP Cloud Platform.Для этого я использую OkhttpClient.Код, используемый для отправки запроса:
private String doGetAsString()
throws IOException {
if (connection == null) {
connect(serverUri);
}
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/json");
try {
Response response = connect(connection);
String body = response.getBody();
Console.printText(String.format("Response [%1$d] %2$s", response.getCode(), body));
return body;
}
finally {
disconnect();
}
}
Код для получения ответа из приведенного выше кода:
private Response connect(HttpURLConnection connection)
throws IOException {
try {
connection.connect();
}
catch (ConnectException e) {
String errorMessage = "Unable to connect. Please check your Internet connection and proxy settings.";
throw new IOException(errorMessage, e);
}
int code = connection.getResponseCode();
InputStream stream;
if (code < HttpURLConnection.HTTP_OK || code >= HttpURLConnection.HTTP_MULT_CHOICE) {
stream = connection.getErrorStream();
}
else {
stream = connection.getInputStream();
}
String body = null;
try {
if (stream == null) {
body = connection.getResponseMessage();
}
else {
body = readString(stream);
}
}
finally {
FileUtil.closeStream(stream);
}
return new Response(code, body);
}
Значение подключения: com.android.okhttp.internal.huc.HttpURLConnectionImpl: https://c432c5b0 -3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap / c432c5b0-3ec2-47ad-bf22-23645fa5e565 / ВГД / ядро / API / v1 / жилец / 727792470 / шлюзы фильтр =protocolId% 20eq% 20 'rest'% 20and% 20status% 20eq% 20'online '% 20and% 20type% 20eq% 20'cloud'
Значение serverUri, которое передается на запрос, составляет https://c432c5b0 -3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap / c432c5b0-3ec2-47ad-bf22-23645fa5e565 / iot / core / api / v1 / tenant / 727792470 / шлюзы? Filter = protocolId eq 'rest 'и status eq' online 'и введите eq' cloud '
Но URI, отправляемый от клиента, выглядит как https://c432c5b0 -3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap/c432c5b0-3ec2-47ad-bf22-23645fa5e565/iot/core/api/v1/tenant/727792470/gateways?filter=protocolId%20eq%20'rest'%20and%20status%20eq%20'online'% 20and% 20type% 20eq% 20'cloud'
Я получаю ожидаемый результат при копировании следующего URL в браузере https://c432c5b0 -3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap / c432c5b0-3ec2-47ad-bf22-23645fa5e565/ ИТН / ядро / API / v1 / жилец / 727792470 / шлюзы? фильтровать = protocolId% 20eq% 20% 27rest% 27% 20and% 20status% 20eq% 20% 27online% 27% 20and% 20type% 20eq% 20% 27cloud% 27
Аутентификация выполняется по имени пользователя и паролю
public void connect(String serverUri)
throws IOException {
this.serverUri = serverUri;
connection = openConnection(serverUri);
if (user != null && password != null) {
//TODO 2
byte[] encodedBytes = android.util.Base64.encode((user + ":" + password).getBytes(), Base64.DEFAULT);
String base64 = new String(encodedBytes, Constants.DEFAULT_ENCODING);
connection.setRequestProperty("Authorization", "Basic " + base64);
}
else if (sslSocketFactory != null && connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
}
else {
throw new IOException("No authorization details provided");
}
}
![enter image description here](https://i.stack.imgur.com/vfnHc.png)