0

I want to send IoT data from an android application to SAP Cloud Platform IoT service. For that, I am using OkhttpClient. The code used to send request is

 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();
    }
}

Code for getting response from the above code is

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);
    }

The value of connection is com.android.okhttp.internal.huc.HttpURLConnectionImpl: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'

The serverUri that is passing to the request is https://c432c5b0-3ec2-47ad-bf22-23645fa5e565.eu10.cp.iot.sap/c432c5b0-3ec2-47ad-bf22-23645fa5e565/iot/core/api/v1/tenant/727792470/gateways?filter=protocolId eq 'rest' and status eq 'online' and type eq 'cloud'

But the uri that send from the client seems like 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'

I get the expected result when coping following url in the browser 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%27rest%27%20and%20status%20eq%20%27online%27%20and%20type%20eq%20%27cloud%27

The authentication is done by username and password

 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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
coolcoder
  • 27
  • 1
  • 9

1 Answers1

0

basically, this happens when your server doesn't support GET and you are using that. but as mentioned you are getting it in browser GET will be working.

I noticed that you are using an HTTPS Uri, did you allow the SSL certificate for the same in your app. else check the below link for adding SSL.

Does OkHttp support accepting self-signed SSL certs?

ASHMIL
  • 521
  • 1
  • 7
  • 23