HTTP request from java business logic

Hi guys,

I have a problem with my custom business logic.
While I’m in debug mode everything works fine, but when I switch into production something goes wrong. I don’t know what, I’m not able to identify any error, but I can’t find any new object into my table after the execution(the code simply retrieve a json, convert it into an object and store this object into a backendless table, in debug mode this works as expected).
It think the problem is related to the way I make the http call to the external host in order to retrieve the json.
The website has been already approved in the external host list.
So, what is the proper way to make an http call from a timer in java custom business logic?

This is my current code:

public static String readJsonFromUrl(String url) throws IOException {
    URL website = new URL(url);
    URLConnection connection = website.openConnection();
    BufferedReader in = new BufferedReader(
            new InputStreamReader(
                    connection.getInputStream()));

    StringBuilder response = new StringBuilder();
    String inputLine;

    while ((inputLine = in.readLine()) != null)
        response.append(inputLine);

    in.close();

    return response.toString();
}

Thanks in advance for your answers!

Hello!

Your issue can happen if url host is not added to “External hosts in Business Logic” section. Please, check the following doc: https://backendless.com/documentation/business-logic/java/bl_external_hosts.htm
Also you can check your code in the following way: put it to another method, not to timer. For example, you can create a hosted service with this method, deploy it to production and try to invoke it. This way you’ll be able to see the server output.
best regards,
Alex

Hi Alexandr,

The url is already an approved host.
I’ll try to move the method inside a hosted service. Thank you for the advice!
I’ll update you as soon as I try.
Regards,

Mirko