【问题标题】:Making a GET call to Sabre对 Sabre 进行 GET 调用
【发布时间】:2017-05-04 06:02:02
【问题描述】:

我正在尝试对 sabre 的 api 进行 GET 调用。我没有收到 我的回复中的数据,我对 httpGet/post/request 还很陌生。

public HttpResponse callGetMethod() {
    HttpResponse response = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI("https://api.sabre.com/v1/shop/flights?origin=ATL&destination=LAS&departuredate=2016-08-13&returndate=2016-08-15&limit=1&enabletagging=true"));
        response = client.execute(request);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return response;
}

我觉得我只是在某个地方错过了一小步。

感谢任何帮助!

【问题讨论】:

  • “但我似乎无法得到我想要的东西”:如果你告诉我们你想要什么,我们可能会提供帮助。什么没有按预期工作?
  • 确保在 Manifest 文件中添加 Internet 权限。这是新手常犯的错误
  • 另外,如果您可以详细说明错误或异常,或者可以粘贴您的日志,那么我们可以提供进一步的帮助
  • 我的回复中没有收到任何数据。我正在使用异步任务,但这只是内部调用的方法。
  • 如果有帮助,GET 格式应如下所示:

标签: android rest api http-get sabre


【解决方案1】:

你可以试试这样的:

string authoriazionToken = "T1RLAQILfFO0MrYdVVePW8z......";
WebRequest webRequest = WebRequest.Create(new URI("https://api.sabre.com/v1/shop/flights?origin=ATL&destination=LAS&departuredate=2016-08-13&returndate=2016-08-15&limit=1&enabletagging=true"));
webRequest.Method =  "GET";
webRequest.Headers.Add("Authorization", "Bearer " + authoriazionToken);
webRequest.Headers.Add("Accept-Encoding", "gzip");
webRequest.ContentType = "application/json";

try
{
    WebResponse webResp = webRequest.GetResponse();
    using (var reader = new System.IO.StreamReader(webResp.GetResponseStream()))
    {
        //Insert parsing code here
    }

}
catch.....

我有一些其他的东西。

【讨论】:

  • 感谢您在这里回答一个老问题!我不在家试一试,但如果成功了,我会告诉你的。
  • 没问题,我最近遇到了像你这样的不同事情,这对我有用:)
猜你喜欢
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2016-06-13
  • 2013-10-08
  • 2018-04-16
  • 1970-01-01
  • 2020-09-13
  • 2013-10-03
相关资源
最近更新 更多