0

我正在使用 axios 使用 Google 的 api 获取 GeoLocation 信息。

var request = require('axios');

request.get(`https://maps.googleapis.com/maps/api/geocode/json?
  latlng=${this.state.location.lat},${this.state.location.long}
  &language=${this.state.language}&result_type=country|administrative_area_level_1
  &key=${keys.googlemaps}`).then((response)=>{/* ... */})

此请求在 iOS 模拟器上运行时有效,但当我在 Android 模拟器中发送此请求时,出现以下异常:

{
   config: ...,
   data: "not valid as a java.net.URI: https://maps.googleapis.com/maps/api/geocode/json?latlng=65.9667,-18.5333&language=en&result_type=country|administrative_area_level_1&key=MY_KEY",
...
}

我已经阅读了其他关于 URL 中的分号在 Android 上引发“作为 java.net.URI 无效”异常的帖子,但这个 URL 没有。使用 fetch 等其他网络 API 时也会出现此问题。

有没有其他人遇到过这个问题?谢谢!

4

1 回答 1

0

尽管我正在使用 fetch,但 encodeURI() 为我解决了“作为 java.net.URI 无效”的问题。

来自:request.get( .....)

到:request.get(encodeURI( .....))

于 2016-03-14T06:40:57.597 回答