0

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Werner der Champ
  • 361
  • 1
  • 3
  • 12

1 Answers1

1

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

dahrens
  • 3,879
  • 1
  • 20
  • 38
  • @daherens: Thank you, that fixed one problem. Now my code looks like this: – Werner der Champ Oct 19 '16 at 19:15
  • `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. – Werner der Champ Oct 19 '16 at 19:17
  • may you add this formatted to your question - nearly unreadable :) – dahrens Oct 19 '16 at 19:22
  • I've fixed the URL problem (it was actually a dumb mistake, but settig up the request still doesn't work (see code in Question) – Werner der Champ Oct 20 '16 at 15:57