【问题标题】:Invalid Request when debuging on Android devide, Xamarin在 Android 设备 Xamarin 上调试时请求无效
【发布时间】:2018-01-24 17:48:23
【问题描述】:

我正在用 Xamarin Forms 编写一个项目,今天我们在 Android 设备上试用我们的应用程序。我们对 googleAPI 的请求停止工作。代码如下:

HttpWebRequest webRequest = WebRequest.Create(@"https://maps.googleapis.com/maps/api/place/search/json?location=" + position.Latitude + "," + position.Longitude + "&radius=1000&types=bar&sensor=false&key=APIKEY") as HttpWebRequest;
webRequest.Method = "GET";
webRequest.BeginGetResponse(new AsyncCallback(RequestCompleted), webRequest);

然后:

var request = (HttpWebRequest)result.AsyncState;
var response = (HttpWebResponse)request.EndGetResponse(result);
using (var stream = response.GetResponseStream())
{
var r = new StreamReader(stream);
var resp = r.ReadToEnd();
}

我们得到响应并将其更改为 json。在计算机上它工作正常,但在 Android 上它说 INVALID REQUEST。有人使用 Xamarin 并知道如何解决它?

【问题讨论】:

  • 你在项目设置/android manifest中添加了上网权限吗?
  • 是的,在 Android 模拟器中一切正常。

标签: android json xamarin webrequest


【解决方案1】:

如果非要我猜的话,我猜你手机上的语言环境/语言与你电脑上的不同。

HttpWebRequest webRequest = WebRequest.Create(@"https://maps.googleapis.com/maps/api/place/search/json?location=" + position.Latitude + "," + position.Longitude + "&radius=1000&types=bar&sensor=false&key=APIKEY") as HttpWebRequest; 

...n=" + position.Latitude + "," + position.Longitude + "&ra... 处有隐含的 ToString(),它们会产生本地化的输出。但是 google API 需要带有 . 作为小数分隔符的值。

追加.ToString("G", CultureInfo.InvariantCulture)

HttpWebRequest webRequest = WebRequest.Create(@"https://maps.googleapis.com/maps/api/place/search/json?location=" + 
position.Latitude.ToString("G", CultureInfo.InvariantCulture) + 
"," + position.Longitude.ToString("G", CultureInfo.InvariantCulture) +
"&radius=1000&types=bar&sensor=false&key=APIKEY") as HttpWebRequest; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 2013-01-23
    • 1970-01-01
    相关资源
    最近更新 更多