Werner der Champ
Werner der Champ

Reputation: 361

Http Request with Google API client library

I'm using Java in order to get some JSON files from an API (Authorisation required), but I haven't found how the set up the request probably.

I'm using the Google API Client Libary (https://developers.google.com/api-client-library/java/google-http-java-client/)

This is my code:

 HttpRequest request=new HttpRequest;
    request.setUrl(url);
    request.setConnectTimeout(5000);
    request.execute();

When compiling, I get the following error:

Unhandled exception: java.io.IOException

Does anybody know, what I'm doing wrong? I've not found any hint in the documentation.

PS: I wouldn't mind switching to another libary. I do need the HTML status code to catch some errors (which I haven't implemented yet).

Edit: thanks to daherens, my code does now look like this

HttpRequest request=new HttpRequest; request.setUrl(url); request.setConnectTimeout(5000); try { request.execute(); } catch (IOException e) { e.printStackTrace(); } Sadly there's still a "( or [ required" Error where 'response' is defined.

Edit2: It says that I need a HTTP Transport objekt. But even when creating it, it just says

'HttpRequest(com.google.api.client.http.HttpTransport, java.lang.String)' is not public in 'com.google.api.client.http.HttpRequest'. Cannot be accessed from outside package

I don't know either what a HTTP Transport object is

Upvotes: 0

Views: 1353

Answers (1)

dahrens
dahrens

Reputation: 3959

In java functions and methods declare which exceptions they might raise and you must deal with that. Either by telling in your method that you'll also throws it, or by catching it yourself with try {} catch. Read more about this here

Upvotes: 1

Related Questions