Android OkHttp3获取完整请求链接Url

 	Request request = new Request.Builder()
                .url(attachHttpGetParams(url, params, isHavFirst))
                .build();
    Logger.e("完整请求连接:"+request.httpUrl().url().toString());

//=====================================================================================//
    /**
     * 为HttpGet 的 url 方便的添加多个name value 参数。
     *
     * @param url
     * @param params
     * @param isHavFirst 是否需要问号
     * @return
     */
    private static String attachHttpGetParams(String url, Map<String, Object> params, boolean isHavFirst) {
        Iterator<String> keys = params.keySet().iterator();
        Iterator<Object> values = params.values().iterator();
        StringBuffer stringBuffer = new StringBuffer();
        if (isHavFirst) {
            stringBuffer.append("?");
        } else {
            stringBuffer.append("&");
        }


        for (int i = 0; i < params.size(); i++) {
            String value = null;
            try {
                value = URLEncoder.encode(values.next().toString(), "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }

            stringBuffer.append(keys.next() + "=" + value);
            if (i != params.size() - 1) {
                stringBuffer.append("&");
            }
        }
//        Logger.e("HttpGetParams = " + url+stringBuffer);//请求连接
        return url + stringBuffer.toString();
    }

你可能感兴趣的:(Android工具类,Android开发)