Back to project page NetworkFacade.
The source code is released under:
Apache License
If you think the Android project NetworkFacade listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.saguinav.networkfacade.sample; //from ww w .j ava2s.co m import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class SimpleURLBuilder { private static final String DEFAULT_CHARSET = "UTF-8"; private final String mScheme; private final String mHost; private String mCharset; private final List<String> mPathSegments = new ArrayList<String>(); private final Map<String, String> mQueryParams = new HashMap<String, String>(); public SimpleURLBuilder(String scheme, String host) { this.mScheme = scheme; this.mHost = host; this.mCharset = DEFAULT_CHARSET; } public SimpleURLBuilder charset(String charset) { this.mCharset = charset; return this; } public SimpleURLBuilder pathSegment(String segment) { mPathSegments.add(segment); return this; } public SimpleURLBuilder pathSegments(String... segments) { for (String segment : segments) { pathSegment(segment); } return this; } public SimpleURLBuilder queryParam(String name, String value) { try { if (value != null) { mQueryParams.put(name, URLEncoder.encode(value, mCharset)); } } catch (UnsupportedEncodingException e) { throw new SimpleURLBuilderException(e); } return this; } public SimpleURLBuilder queryParam(String name, String[] values, String separator) { final StringBuilder valueBuilder = new StringBuilder(); for (String value : values) { try { if (value == null) continue; final String encodedValue = URLEncoder.encode(value, mCharset); if (valueBuilder.length() > 0) { valueBuilder.append(separator); } valueBuilder.append(encodedValue); } catch (UnsupportedEncodingException e) { throw new SimpleURLBuilderException(e); } } mQueryParams.put(name, valueBuilder.toString()); return this; } public SimpleURLBuilder queryParams(Map<String, String> queryParams) { this.mQueryParams.putAll(queryParams); return this; } public String build() { final StringBuilder urlBuilder = new StringBuilder(); urlBuilder.append(mScheme); urlBuilder.append("://"); urlBuilder.append(mHost); for (String pathSegment : mPathSegments) { urlBuilder.append('/'); urlBuilder.append(pathSegment); } if (!mQueryParams.isEmpty()) { urlBuilder.append("?"); final Iterator<Map.Entry<String, String>> queryParamsIterator = mQueryParams.entrySet().iterator(); while (queryParamsIterator.hasNext()) { final Map.Entry<String, String> queryParam = queryParamsIterator.next(); boolean addSeparator = true; if (queryParam.getKey() != null && queryParam.getValue() != null) { urlBuilder.append(queryParam.getKey()); urlBuilder.append('='); urlBuilder.append(queryParam.getValue()); } else { addSeparator = false; } if (queryParamsIterator.hasNext()) { if (addSeparator) urlBuilder.append('&'); } } } try { final URL url = new URL(urlBuilder.toString()); // http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()).toString(); } catch (URISyntaxException e) { throw new SimpleURLBuilderException(e); } catch (MalformedURLException e) { throw new SimpleURLBuilderException(e); } } public static class SimpleURLBuilderException extends RuntimeException { public SimpleURLBuilderException(Throwable throwable) { super(throwable); } } }