简体   繁体   English

带有Google API客户端库的Http请求

[英]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.

相关问题 使用Google Java API客户端库更改批HTTP请求的READ TIMEOUT - Change READ TIMEOUT for a batch HTTP Request on using Google API Client Library for Java 使用Google API中的com.google.api.client.http.HttpRequest对象发送POST请求 - Sending POST request using com.google.api.client.http.HttpRequest object in Google API 将代理与Google HTTP Client Library for Java结合使用 - Using proxies with Google HTTP Client Library for Java Google HTTP Java Client的POST请求错误 - POST Request Error with Google HTTP Java Client 在使用 com.google.api.client.http.HttpRequest 的 POST 请求上需要帮助 - Need help on POST request using com.google.api.client.http.HttpRequest 如何使用Google Translate v2 API客户端库为java提出翻译请求? - How do you make a request for a translation with the Google Translate v2 API Client Library for java? 使用Java中的Google Translate V2 API客户端库发送翻译请求 - sending a request for a translation using Google Translate V2 API Client Library in java Google Drive API Java客户端库日志记录 - Google Drive API Java Client Library Logging com.google.api.client库中的NoSuchMethodError - NoSuchMethodError in com.google.api.client library 向Google发送的HTTP请求通过航点映射api方向 - A http request to google maps api direction with waypoints
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM