Android Open Source - khandroid Request Builder






From Project

Back to project page khandroid.

License

The source code is released under:

Apache License

If you think the Android project khandroid 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.github.khandroid.volley;
/* w  w w .j ava  2  s .  c o m*/
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.android.volley.Request;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import khandroid.ext.apache.http.NameValuePair;
import khandroid.ext.apache.http.client.utils.URIBuilder;
import khandroid.ext.apache.http.client.utils.URLEncodedUtils;
import khandroid.ext.apache.http.message.BasicNameValuePair;


abstract public class RequestBuilder<T> {
    private String mProtocol;
    private String mDomain;
    private String mPath;
    private int mPort;

    private Listener<T> mSuccessListener;
    private ErrorListener mErrorListener;

    private Map<String, String> mGetParams = new HashMap<String, String>();

    private static final String CHARSET = "UTF-8";


    public RequestBuilder(String url) {
        super();

        if (url != null) {
            URI tmpUri;
            try {
                tmpUri = new URI(url);
            } catch (URISyntaxException e) {
                throw new IllegalArgumentException("Invalid URL: " + url, e);
            }

            setProtocol(tmpUri.getScheme());
            setDomain(tmpUri.getHost());
            setPath(tmpUri.getPath());
            setPort(tmpUri.getPort());

            for (NameValuePair pair : URLEncodedUtils.parse(tmpUri, CHARSET)) {
                mGetParams.put(pair.getName(), pair.getValue());
            }
        } else {
            throw new IllegalArgumentException("URL is null.");
        }
    }


    protected String buildUriString() {
        String ret;

        String protocol = getProtocol();
        if (protocol != null && !protocol.equals("") && getDomain() != null
                && !getDomain().equals("") && getPath() != null && !getPath().equals("")) {
            URI uri;
            try {
                URIBuilder ub = new URIBuilder();
                ub.setScheme(protocol).setHost(getDomain()).setPort(getPort()).setPath(getPath());
                ub.setQuery(URLEncodedUtils.format(prepareGetParameters(), "UTF-8"));
                uri = ub.build();
            } catch (URISyntaxException e) {
                throw new IllegalStateException("Error creating URI.", e);
            }

            ret = uri.toString();
        } else {
            throw new IllegalStateException("Some of required fields (protocol, domain, path) are empty.");
        }

        return ret;
    }


    public ArrayList<NameValuePair> prepareGetParameters() {
        ArrayList<NameValuePair> ret = new ArrayList<NameValuePair>();

        for (Map.Entry<String, String> entry : mGetParams.entrySet()) {
            ret.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }

        return ret;
    }


    abstract public Request<?> build();


    public void setParameter(String key, String value) {
        mGetParams.put(key, value);
    }


    public Map<String, String> getGetParams() {
        return mGetParams;
    }


    public String getProtocol() {
        return mProtocol;
    }


    public void setProtocol(String protocol) {
        String protoLower = protocol.toLowerCase(Locale.ENGLISH);
        if (protoLower.equals("http") || protoLower.equals("https")) {
            this.mProtocol = protocol;
        } else {
            throw new IllegalArgumentException("Protocol must be http ot https");
        }
    }


    public String getDomain() {
        return mDomain;
    }


    public void setDomain(String domain) {
        this.mDomain = domain;
    }


    public String getPath() {
        return mPath;
    }


    public void setPath(String path) {
        this.mPath = path;
    }


    public int getPort() {
        return mPort;
    }


    public void setPort(int port) {
        this.mPort = port;
    }


    public void setSuccessListener(Listener<T> successListener) {
        mSuccessListener = successListener;
    }


    public void setErrorListener(ErrorListener errorListener) {
        mErrorListener = errorListener;
    }


    protected Listener<T> getSuccessListener() {
        return mSuccessListener;
    }


    protected ErrorListener getErrorListener() {
        return mErrorListener;
    }

}




Java Source Code List

com.github.khandroid.JsonFunctionality.java
com.github.khandroid.http.functionality.HttpFunctionalityImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookiesImpl.java
com.github.khandroid.http.functionality.HttpFunctionalityWCookies.java
com.github.khandroid.http.functionality.HttpFunctionality.java
com.github.khandroid.http.misc.FileDownloader.java
com.github.khandroid.http.misc.KhandroidBasicResponseHandler.java
com.github.khandroid.http.misc.SynchronizedCookieStore.java
com.github.khandroid.http.request.GetRequestBuilder.java
com.github.khandroid.http.request.PostRequestBuilder.java
com.github.khandroid.http.request.RequestBuilder.java
com.github.khandroid.http.ssl.DefaultSslHttpClient.java
com.github.khandroid.http.ssl.KhandroidX509TrustManager.java
com.github.khandroid.http.ssl.SslSocketFactory.java
com.github.khandroid.kat.ActivityKatExecutorFunctionality.java
com.github.khandroid.kat.FragmentKatExecutorFunctionality.java
com.github.khandroid.kat.KatExecutor.java
com.github.khandroid.kat.KhandroidAsyncTask.java
com.github.khandroid.misc.ActivityUtils.java
com.github.khandroid.misc.DateTimeHelper.java
com.github.khandroid.misc.ImageUtils.java
com.github.khandroid.misc.LocationUtils.java
com.github.khandroid.misc.NetUtils.java
com.github.khandroid.misc.StringUtils.java
com.github.khandroid.misc.ViewUtils.java
com.github.khandroid.rest.InvalidJsonString.java
com.github.khandroid.rest.InvalidResponseException.java
com.github.khandroid.rest.KhRestExchangeBuilder.java
com.github.khandroid.rest.KhRestFunctionalityImpl.java
com.github.khandroid.rest.KhRestFunctionality.java
com.github.khandroid.rest.KhRestResult.java
com.github.khandroid.rest.MalformedResponseException.java
com.github.khandroid.rest.RawRestResult.java
com.github.khandroid.rest.RestAsyncTask.java
com.github.khandroid.rest.RestExchangeBuilder.java
com.github.khandroid.rest.RestExchangeFailedException.java
com.github.khandroid.rest.RestExchangeOutcome.java
com.github.khandroid.rest.RestExchange.java
com.github.khandroid.rest.RestFragmentFunctionality.java
com.github.khandroid.rest.RestFunctionalityImpl.java
com.github.khandroid.rest.RestFunctionality.java
com.github.khandroid.rest.RestPersistFragment.java
com.github.khandroid.rest.RestResponse.java
com.github.khandroid.rest.RestResult.java
com.github.khandroid.rest.UnexpectedResponseException.java
com.github.khandroid.state.StateEvent.java
com.github.khandroid.state.StateMachineImpl.java
com.github.khandroid.state.StateMachine.java
com.github.khandroid.state.StateSkeleton.java
com.github.khandroid.state.State.java
com.github.khandroid.state_app.AppStateContextImpl.java
com.github.khandroid.state_app.AppStateContext.java
com.github.khandroid.state_app.AppState.java
com.github.khandroid.state_app.InvalidAppStateException.java
com.github.khandroid.state_app.SkeletonAppState.java
com.github.khandroid.state_app.StateEvent.java
com.github.khandroid.ui_functionality.ActivityAttachable.java
com.github.khandroid.ui_functionality.ActivityAttachedFunctionality.java
com.github.khandroid.ui_functionality.ActivityUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentAttachable.java
com.github.khandroid.ui_functionality.FragmentAttachedFunctionality.java
com.github.khandroid.ui_functionality.FragmentUniqueAttachedFunctionality.java
com.github.khandroid.ui_functionality.HostActivity.java
com.github.khandroid.ui_functionality.HostFragment.java
com.github.khandroid.ui_functionality.SuperNotCalledException.java
com.github.khandroid.views.ImageViewWithContextMenuInfo.java
com.github.khandroid.volley.GetJsonObjectRequestBuilder.java
com.github.khandroid.volley.GetStringRequestBuilder.java
com.github.khandroid.volley.GetVRestRequestBuilder.java
com.github.khandroid.volley.PostJsonObjectRequestBuilder.java
com.github.khandroid.volley.PostRequestBuilderImpl.java
com.github.khandroid.volley.PostRequestBuilder.java
com.github.khandroid.volley.PostStringRequestBuilder.java
com.github.khandroid.volley.PostVRestRequestBuilder.java
com.github.khandroid.volley.RequestBuilder.java
com.github.khandroid.volley.VRestRequestBuilder.java
com.github.khandroid.volley.VRestRequest.java