Xamarin android无效的uri异常

时间:2016-10-17 16:02:34

标签: android xamarin.android httpwebrequest uri

在我的Xamarin android应用程序中,我需要根据用户的当前位置对openweathermap进行api调用以获取当前的天气数据。

这是我的查询字符串代码:

url = "api.openweathermap.org/data/2.5/weather?lat=" +
                                _currentLocation.Latitude +
                                "&lon=" +
                                _currentLocation.Longitude +
                                "&APPID=" + openWeatherAPIKey
                                                + "&units=imperial";

现在在方法FetchUrlAsync中,我发送Web请求并检索响应json。以下是该方法的代码:

async Task<JsonValue> FetchUrlAsync(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri(url));
        request.ContentType = "application/json";
        request.Method = "GET";

        using (WebResponse response = await request.GetResponseAsync())
        {
            // Get a stream representation of the HTTP web response:
            using (System.IO.Stream stream = response.GetResponseStream())
            {
                // Use this stream to build a JSON document object:
                JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());

                // Return the JSON document:
                return jsonDoc;
            }
        }
    }

我在方法的第一行抛出了一个System.URIFormatException,因为uri的格式错误。我检查了其他堆栈溢出问题,我认为这可能是因为我的url字符串不是以http://或ftp开头的。但是我无法在url字符串前面添加http://或ftp,因为这会导致无法识别的服务器。

此问题的解决方案是什么?或者它与添加http://或ftp前缀无关?

0 个答案:

没有答案