Java實現PHP中的http_build_query()效果

 /**
     * Java實現PHP中的http_build_query()效果
     * @param array
     *        key=value形式的二位數組
     * @return
     */
    public static String http_build_query(Map<String,Object> array){
        String reString = null;
        //遍歷數組形成akey=avalue&bkey=bvalue&ckey=cvalue形式的的字符串
        Iterator it = array.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String,Object> entry =(Map.Entry) it.next();
            //中間加個過濾,去掉token和checksum
//            if("token".equals(entry.getKey())||"checksum".equals(entry.getKey())){
//                continue;
//            }
            String key = entry.getKey();
            Object value = entry.getValue();
            reString += key+"="+value+"&";
        }
        reString = reString.substring(0, reString.length()-1);
        //將得到的字符串進行處理得到目標格式的字符串
        reString = java.net.URLEncoder.encode(reString);
        reString = reString.replace("%3D", "=").replace("%26", "&");
        return reString;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章