所以我想從我的Android應用程序中使用Xamarin的API(我無法控制)獲取一些信息。API請求在應用程序中失敗,但在瀏覽器中工作
它的工作原理!如果我將生成的鏈接粘貼到Chrome中,或者使用Chrome擴展郵件管理器,但我無法在我的應用程序中正常工作,我會得到我想要的信息。
應用程序處於poc狀態,因此一切都在主它實現了ILocationListener
並請求GPS每3秒更新,但在這裏活動是代碼的有趣的部分:
public void OnLocationChanged(Location location) {
var lat = location.Latitude;
var lon = location.Longitude;
_lat.Text = "lat: " + lat;
_lon.Text = "lon: " + lon;
// get the vegReference (roadReference) based on the lat & lon
var url = "https://www.vegvesen.no/nvdb/api/vegreferanse/koordinat?lon=" + lon.ToString("") + "&lat=" + lat.ToString("") + "&geometri=WGS84";
// ex: https://www.vegvesen.no/nvdb/api/vegreferanse/koordinat?lon=10.399547&lat=63.354658&geometri=WGS84
var json = new JSONObject(Get(url));
Log.Debug("url", url);
Log.Debug("json", json.ToString());
if (json.GetString("kommuneNr") == "0") return; // bug in API?
var kommuneNr = json.GetString("kommuneNr");
var vegReferanse = json.GetString("visningsNavn");
// get the object id 105 (speed limit) on set vegReference
// API dok: https://www.vegvesen.no/nvdb/apidokumentasjon/#/get/vegobjekter
var url2 = "https://www.vegvesen.no/nvdb/api/v2/vegobjekter/105?vegreferanse=" + vegReferanse.Replace(" ", "") + "&inkluder=lokasjon&segmentering=false";
// ex. https://www.vegvesen.no/nvdb/api/v2/vegobjekter/105?vegreferanse=PV3133HP1m73&inkluder=lokasjon&segmentering=false
// this (url2) is the one that fails
Log.Debug("url2", url2);
var json2 = new JSONObject(Get(url2));
Log.Debug("json2", json2.ToString());
// get the speed limit with the heighest id (this should be the latest one (FYI: speedlimits have changed over the years, all speed limits are in the database for historical reasons(?)))
var objs = json2.GetJSONArray("vegObjekter");
var list = new List<JSONObject>();
// getting a list of possible objects based on the kommuneNr (because for some f*ced up reason the result returns objects in other kommunes aswell...)
for (int i = 0; i < objs.Length() - 1; i++)
{
if (objs.GetJSONObject(i).GetJSONObject("lokasjon").GetJSONArray("kommuner").GetString(0) == kommuneNr)
list.Add(objs.GetJSONObject(i));
}
if (list.Count == 0) return;
var url3 = list[list.Count - 1].GetString("href");
// ex. https://www.vegvesen.no/nvdb/api/v2/vegobjekter/105/276790644
Log.Debug("url3", url3);
var json3 = new JSONObject(Get(url3));
Log.Debug("json3", json3.ToString());
// set the speed-limit to the textview.
var res = json3.GetJSONArray("egenskaper").GetJSONObject(0).GetString("verdi");
_tvSl.Text = res + " km/t";
}
而且Get
- 方法beeing稱爲OnLocationChanged
內,基本上只獲得響應作爲一個字符串:
private string Get(string url)
{
var request = WebRequest.Create(url);
var sb = new StringBuilder();
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
sb.Append(sr.ReadToEnd());
}
return sb.ToString();
}
從URL2-請求的響應是這樣的:
System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
我的問題是;爲什麼鏈接(ref url2)在瀏覽器中運行,而不是在應用程序中運行。
任何幫助或指針在這個問題將不勝感激! 另請參見:鏈接到GitHub上的項目:https://github.com/Nemeas/alfaOmega
嘿!這裏的API作者之一。我也想深入瞭解這個案例。我無法在這個時候運行你的Xamarin代碼,但我想指導你編程參考。我們已經爲Java平臺發佈了一個開源客戶端。源代碼可能會給你的索姆提示https://github.com/nvdb-vegdata/nvdb-api-client – matsa
@matsa所以我實際上(最終)通過添加'接受:應用程序/ json的工作得到的東西工作請求。但是我不得不在'Get'方法中使用'HttpWebRequest',因爲'request.Headers.Add(HttpRequestHeader.Accept,「application/json」)'不起作用(它一直說我必須提供一個有效的屬性值,或者類似的東西。還嘗試使用字符串值「accept」而不是Enum/Const(?)HttpRequestHeader.Accept),但是'HttpWebRequest'暴露了'accept'作爲一個屬性,如下所示:'request.Accept = 「應用/ JSON」'。這在第一個url中並不需要apiv1 – Nemeas