Retrofit關(guān)閉GET方法中的Query注解參數(shù)Url編碼

百度逆地理位置可以將我們從手機(jī)中獲取的大地坐標(biāo)系統(tǒng)中得到的gps坐標(biāo)轉(zhuǎn)為行政單位,方便顯示給用戶。但是在使用retrofit來(lái)訪問(wèn)的這個(gè)api的過(guò)程中出現(xiàn)了一段小波折。最開始我是通過(guò)下面的方法來(lái)構(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);

然后在手機(jī)中訪問(wèn),在日志中觀察實(shí)際上訪問(wèn)的是下面的這個(gè)鏈接。其實(shí)不能正確訪問(wèn),因?yàn)樯婕暗焦救舾尚畔?,所以u(píng)rl中的參數(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

上面的這個(gè)經(jīng)過(guò)url編碼之后的鏈接,百度逆地理api似乎不認(rèn)識(shí),于是就返回來(lái)下面的結(jié)果。

{
    "status": 200,
    "message": "APP不存在,AK有誤請(qǐng)檢查再重試"
}

于是我就想,難道android訪問(wèn)的時(shí)候,是都會(huì)將一些特殊符號(hào)轉(zhuǎn)為url編碼嗎?上面鏈接中的%3A實(shí)際上是":"冒號(hào)。那怎么才能防止冒號(hào)被轉(zhuǎn)為%3A呢?于是我就測(cè)試了一下HttpUrlConnection來(lái)訪問(wèn)這個(gè)接口

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();
        }

上面的訪問(wèn)就沒(méi)有任何問(wèn)題,于是我就想,既然默認(rèn)的get訪問(wèn)api不會(huì)經(jīng)過(guò)url編碼,那么retrofit卻自動(dòng)url編碼了,那么應(yīng)該就會(huì)提供關(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。然后解決問(wèn)題。

@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: "北京市朝陽(yáng)區(qū)",
        business: "四惠,十里堡,甘露園",
        addressComponent: {
            country: "中國(guó)",
            country_code: 0,
            province: "北京市",
            city: "北京市",
            district: "朝陽(yáng)區(qū)",
            adcode: "110105",
            street: "",
            street_number: "",
            direction: "",
            distance: ""
        },
        pois: [
            
        ],
        roads: [
            
        ],
        poiRegions: [
            
        ],
        sematic_description: "中瑞匯通大廈內(nèi)0米",
        cityCode: 131
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容