百度逆地理位置可以將我們從手機中獲取的大地坐標(biāo)系統(tǒng)中得到的gps坐標(biāo)轉(zhuǎn)為行政單位券盅,方便顯示給用戶杂靶。但是在使用retrofit來訪問的這個api的過程中出現(xiàn)了一段小波折。最開始我是通過下面的方法來構(gòu)造url的虱肄。
@GET("/v2/")
Observable<BaiduLocationBean> getBaiduLocation(@Query("location") String location,
@Query("output")String output,@Query("pois") String pois,
@Query("ak") String ak,@Query( "mcode") String mCode,
@Query("coordtype") String coordtype);
然后在手機中訪問谜叹,在日志中觀察實際上訪問的是下面的這個鏈接。其實不能正確訪問括堤,因為涉及到公司若干信息碌秸,所以url中的參數(shù)有刪減。
http://api.map.baidu.com/geocoder/v2/悄窃?location=39.910113,116.502766&output=json&pois=0&ak=bu2lzAsplxBycu0Rz92nryd8I&mcode=1E%3A1E%3ABA%3AF3%3A36%3A71%3AAC%3AE5%3AA9%3A37%3ADE%3A88%3A84%3A52%3A97%3A8C%3AE8%3Bcom.dzy&coordtype=wgs84ll
上面的這個經(jīng)過url編碼之后的鏈接讥电,百度逆地理api似乎不認識,于是就返回來下面的結(jié)果轧抗。
{
"status": 200,
"message": "APP不存在恩敌,AK有誤請檢查再重試"
}
于是我就想,難道android訪問的時候横媚,是都會將一些特殊符號轉(zhuǎn)為url編碼嗎纠炮?上面鏈接中的%3A實際上是":"冒號。那怎么才能防止冒號被轉(zhuǎn)為%3A呢灯蝴?于是我就測試了一下HttpUrlConnection來訪問這個接口
String baseUrl="http://api.map.baidu.com/geocoder/v2/?location="+latitude+","+longitude+
"&output=json&pois=0&ak=bu2lzAsplxBycu0Rz92nryd8I&" +
"mcode=1E:1E:BA:F3:36:71:AC:E5:A9:37:DE:88:84:52:97:8C:E8;com.dzy&" +
"coordtype=wgs84ll";
try {
URL url=new URL(baseUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
if(urlConnection.getResponseCode()>=200&&urlConnection.getResponseCode()<=300) {
InputStream stream = urlConnection.getInputStream();
byte[] buffer = new byte[2048];
int readBytes = 0;
StringBuilder stringBuilder = new StringBuilder();
while ((readBytes = stream.read(buffer)) != -1) {
stringBuilder.append(new String(buffer, 0, readBytes));
}
Log.e("baidu", stringBuilder.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
上面的訪問就沒有任何問題恢口,于是我就想,既然默認的get訪問api不會經(jīng)過url編碼穷躁,那么retrofit卻自動url編碼了耕肩,那么應(yīng)該就會提供關(guān)閉url編碼參數(shù)的開關(guān)。于是看了官方文檔:
Parameter names and values are URL encoded by default. Specify encoded=true
to change this behavior.
@GET("/friends") Call<ResponseBody> friends(@Query(value="group", encoded=true) String group);
但是發(fā)現(xiàn)置為true问潭,并不好用猿诸,于是encoded=false。然后解決問題狡忙。
@GET("/v2/")
Observable<BaiduLocationBean> getBaiduLocation(@Query(value = "location",encodeValue = false) String location,
@Query("output")String output,@Query("pois") String pois,
@Query("ak") String ak,@Query(value = "mcode",encodeValue = false) String mCode,
@Query("coordtype") String coordtype);
http://api.map.baidu.com/geocoder/v2/?location=39.910149,116.502832&output=json&pois=0&ak=bu2lzAsplxBycu0Rz92nryd8I&mcode=1E:1E:BA:F3:36:71:AC:E5:A9:37:DE:88:84:52:97:8C:E8;com.dzy&coordtype=wgs84ll
{
status: 0,
result: {
location: {
lng: 116.51536122516896,
lat: 39.917671134759644
},
formatted_address: "北京市朝陽區(qū)",
business: "四惠,十里堡,甘露園",
addressComponent: {
country: "中國",
country_code: 0,
province: "北京市",
city: "北京市",
district: "朝陽區(qū)",
adcode: "110105",
street: "",
street_number: "",
direction: "",
distance: ""
},
pois: [
],
roads: [
],
poiRegions: [
],
sematic_description: "中瑞匯通大廈內(nèi)0米",
cityCode: 131
}
}