[英]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. 我使用Java是为了从API中获取一些JSON文件(需要授权),但是我还没有发现如何设置请求。
I'm using the Google API Client Libary ( https://developers.google.com/api-client-library/java/google-http-java-client/ ) 我正在使用Google API客户端库( 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. PS:我不介意切换到另一个库。 I do need the HTML status code to catch some errors (which I haven't implemented yet).
我确实需要HTML状态代码来捕获一些错误(尚未实现)。
Edit: thanks to daherens, my code does now look like this 编辑:感谢daherens,我的代码现在看起来像这样
HttpRequest request=new HttpRequest; request.setUrl(url); request.setConnectTimeout(5000); try { request.execute(); } catch (IOException e) { e.printStackTrace(); }
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. HttpRequest request=new HttpRequest; request.setUrl(url); request.setConnectTimeout(5000); try { request.execute(); } catch (IOException e) { e.printStackTrace(); }
可悲的是,在定义“响应”的地方仍然出现“(或[必填”)错误。
Edit2: It says that I need a HTTP Transport objekt. Edit2:它说我需要一个HTTP传输对象。 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 我也不知道什么是HTTP传输对象
In java functions and methods declare which exceptions they might raise and you must deal with that. 在Java函数和方法中,声明它们可能引发哪些异常,您必须对其进行处理。 Either by telling in your method that you'll also throws it, or by catching it yourself with try {} catch.
通过在您的方法中告诉您也将其抛出,或通过try {} catch自己捕获它。 Read more about this here
在这里阅读更多关于此的信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.