Android Open Source - NetworkFacade Simple U R L Builder






From Project

Back to project page NetworkFacade.

License

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.

Java Source Code

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

    }

}




Java Source Code List

com.android.volley.toolbox.AuthenticationChallengesProofHurlStack.java
com.saguinav.networkfacade.ApplicationTest.java
com.saguinav.networkfacade.HttpBody.java
com.saguinav.networkfacade.HttpClient.java
com.saguinav.networkfacade.HttpError.java
com.saguinav.networkfacade.HttpRequest.java
com.saguinav.networkfacade.RetryPolicy.java
com.saguinav.networkfacade.sample.HeadlessFragment.java
com.saguinav.networkfacade.sample.HttpClientSingleton.java
com.saguinav.networkfacade.sample.MainActivity.java
com.saguinav.networkfacade.sample.OkHttpApplication.java
com.saguinav.networkfacade.sample.OkHttpClient.java
com.saguinav.networkfacade.sample.RepositoriesDeserializer.java
com.saguinav.networkfacade.sample.Repository.java
com.saguinav.networkfacade.sample.SimpleURLBuilder.java
com.saguinav.networkfacade.sample.VolleyApplication.java
com.saguinav.networkfacade.sample.VolleyHttpClient.java