2017-05-04 59 views
0

我正在尝试对saber的api进行GET调用。我在回复中没有收到 的数据,而我对httpGet/post/request的新增功能还是比较新的。与Sabre进行GET调用

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; 
} 

我觉得我错过了一个小小的步骤。

任何帮助表示赞赏!

+2

“但是我似乎无法得到我所追求的东西”:如果你告诉我们你可能会帮助我们之后的情况。什么不按预期工作? – Henry

+1

确保在Manifest文件中添加Internet权限。这对于新手来说是一个常见的错误 –

+1

另外,如果您可以详细说明错误或异常,或者可以粘贴您的日志,那么我们可以进一步帮助 –

回答

0

你可以尝试这样的事情:

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..... 

我有那种东西别的东西。

+0

感谢您回答这里的一个老问题!我不在家试一试,但我会让你知道这件事是否成功。 –

+0

不是问题,我最近面对的是与你不同的事情,这对我有用:) – Wisdoom