Search in sources :

Example 1 with StringPart

use of cn.jeesoft.qa.libcore.http.part.StringPart in project QuickAndroid by ImKarl.

the class OkHttp method parseGetUrl.

/**
     * 解析GET方式URL(补充URL请求参数)
     *
     * @param url
     * @param params
     * @return 返回处理后的URL
     */
private static String parseGetUrl(String url, Map<String, List<Part>> params) {
    StringBuilder sb = new StringBuilder();
    if (params == null) {
        params = new HashMap<String, List<Part>>();
    }
    if (url.contains("?")) {
        if (url.endsWith("&")) {
            sb.append(url.substring(0, url.length() - 1));
        }
    } else {
        sb.append(url + "?");
    }
    for (Entry<String, List<Part>> entry : params.entrySet()) {
        String name = entry.getKey();
        List<Part> parts = entry.getValue();
        try {
            if (!TextUtils.isEmpty(name) && !QAStringUtils.isEmpty(parts)) {
                for (Part part : parts) {
                    if (part instanceof StringPart) {
                        sb.append("&" + URLEncoder.encode(part.name(), "UTF-8") + "=" + URLEncoder.encode(((StringPart) part).value(), "UTF-8"));
                    }
                }
            }
        } catch (UnsupportedEncodingException e) {
            QALog.e(e);
        }
    }
    return sb.toString().replace("?&", "?");
}
Also used : StringPart(cn.jeesoft.qa.libcore.http.part.StringPart) Part(cn.jeesoft.qa.libcore.http.part.Part) StringPart(cn.jeesoft.qa.libcore.http.part.StringPart) UnsupportedEncodingException(java.io.UnsupportedEncodingException) List(java.util.List)

Aggregations

Part (cn.jeesoft.qa.libcore.http.part.Part)1 StringPart (cn.jeesoft.qa.libcore.http.part.StringPart)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 List (java.util.List)1