2017-05-04 67 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