Android Open Source - ElyTheme Silk Http Client






From Project

Back to project page ElyTheme.

License

The source code is released under:

GNU General Public License

If you think the Android project ElyTheme 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.afollestad.silk.http;
//w  ww . jav  a 2 s.  com
import android.content.Context;
import android.os.Handler;
import ch.boye.httpclientandroidlib.client.methods.HttpDelete;
import ch.boye.httpclientandroidlib.client.methods.HttpGet;
import ch.boye.httpclientandroidlib.client.methods.HttpPost;
import ch.boye.httpclientandroidlib.client.methods.HttpPut;

/**
 * A Apache HTTP Client wrapper that makes http faster and easier.
 *
 * @author Aidan Follestad (afollestad)
 */
public class SilkHttpClient extends SilkHttpBase {

    public interface ResponseProcessor {
        public void onProcessError(SilkHttpException exception) throws Exception;
    }

    private String mHost;

    /**
     * Initializes a new SilkHttpClient.
     */
    public SilkHttpClient(Context context) {
        super(context);
    }

    /**
     * Initializes a new SilkHttpClient.
     *
     * @param handler The handler used to post to a thread for async callbacks.
     */
    public SilkHttpClient(Context context, Handler handler) {
        super(context, handler);
    }

    private String getUrl(String url) {
        if ((url == null || url.trim().isEmpty()) && (mHost == null || mHost.trim().isEmpty()))
            throw new RuntimeException("The URL cannot be null.");
        if (mHost != null) {
            if (url == null) return mHost;
            if (!url.startsWith("/")) url = "/" + url;
            return mHost + url;
        }
        return url;
    }

    /**
     * Sets a host that is automatically put before every request's URL or used as a request's URL if a null string is passed
     * to GET/POST/PUT/DELETE...
     */
    public SilkHttpClient setHost(String host) {
        if (host != null) {
            host = host.trim();
            if (host.isEmpty()) host = null;
            else {
                if (host.endsWith("/"))
                    host = host.substring(0, host.length() - 1);
                if (!host.startsWith("http://") && !host.startsWith("https://"))
                    host = "http://" + host;
            }
        }
        mHost = host;
        return this;
    }

    /**
     * Adds an HTTP header to the client, which will be used for the next request. Headers are cleared after a request is performed.
     */
    public SilkHttpClient addHeader(SilkHttpHeader header) {
        super.mHeaders.add(header);
        return this;
    }

    /**
     * Adds an HTTP header to the client, which will be used for the next request. Headers are cleared after a request is performed.
     */
    public SilkHttpClient addHeader(String name, String value) {
        if (name == null || name.trim().isEmpty())
            throw new RuntimeException("The header name cannot be null.");
        addHeader(new SilkHttpHeader(name, value));
        return this;
    }

    /**
     * Makes a GET request on the calling thread.
     */
    public SilkHttpResponse get(String url) throws Exception {
        return performRequest(new HttpGet(getUrl(url)));
    }

    /**
     * Makes a POST request on the calling thread.
     */
    public SilkHttpResponse post(String url) throws Exception {
        return post(url, null);
    }

    /**
     * Makes a POST request on the calling thread, with a POST entity (body).
     */
    public SilkHttpResponse post(String url, SilkHttpBody body) throws Exception {
        HttpPost post = new HttpPost(getUrl(url));
        if (body != null) post.setEntity(body.getEntity());
        return performRequest(post);
    }

    /**
     * Makes a PUT request on the calling thread.
     */
    public SilkHttpResponse put(String url) throws Exception {
        return put(url, null);
    }

    /**
     * Makes a GET request on the calling thread, with a PUT entity (body).
     */
    public SilkHttpResponse put(String url, SilkHttpBody body) throws Exception {
        HttpPut post = new HttpPut(getUrl(url));
        if (body != null) post.setEntity(body.getEntity());
        return performRequest(post);
    }

    /**
     * Makes a DELETE request on the calling thread.
     */
    public SilkHttpResponse delete(String url) throws Exception {
        return performRequest(new HttpDelete(getUrl(url)));
    }

    // Async methods

    /**
     * Makes a GET request from a separate thread and posts the results to a callback.
     */
    public void getAsync(final String url, final SilkHttpCallback callback) {
        if (callback == null) throw new IllegalArgumentException("The callback cannot be null.");
        runOnPriorityThread(new Runnable() {
            @Override
            public void run() {
                try {
                    final SilkHttpResponse response = get(url);
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onComplete(response);
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onError(e);
                        }
                    });
                }
            }
        });
    }

    /**
     * Makes a POST request from a separate thread and posts the results to a callback.
     */
    public void postAsync(String url, SilkHttpCallback callback) {
        if (callback == null) throw new IllegalArgumentException("The callback cannot be null.");
        postAsync(url, null, callback);
    }

    /**
     * Makes a POST request from a separate thread (with a POST entity, or body) and posts the results to a callback.
     */
    public void postAsync(final String url, final SilkHttpBody body, final SilkHttpCallback callback) {
        if (callback == null) throw new IllegalArgumentException("The callback cannot be null.");
        runOnPriorityThread(new Runnable() {
            @Override
            public void run() {
                try {
                    final SilkHttpResponse response = post(url, body);
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onComplete(response);
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onError(e);
                        }
                    });
                }
            }
        });
    }

    /**
     * Makes a PUT request from a separate thread and posts the results to a callback.
     */
    public void putAsync(String url, SilkHttpCallback callback) {
        if (callback == null) throw new IllegalArgumentException("The callback cannot be null.");
        putAsync(url, null, callback);
    }

    /**
     * Makes a PUT request from a separate thread (with a PUT entity, or body) and posts the results to a callback.
     */
    public void putAsync(final String url, final SilkHttpBody body, final SilkHttpCallback callback) {
        if (callback == null) throw new IllegalArgumentException("The callback cannot be null.");
        runOnPriorityThread(new Runnable() {
            @Override
            public void run() {
                try {
                    final SilkHttpResponse response = put(url, body);
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onComplete(response);
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onError(e);
                        }
                    });
                }
            }
        });
    }

    /**
     * Makes a DELETE request from a separate thread and posts the results to a callback.
     */
    public void deleteAsync(final String url, final SilkHttpCallback callback) {
        if (callback == null) throw new IllegalArgumentException("The callback cannot be null.");
        runOnPriorityThread(new Runnable() {
            @Override
            public void run() {
                try {
                    final SilkHttpResponse response = delete(url);
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onComplete(response);
                        }
                    });
                } catch (final Exception e) {
                    e.printStackTrace();
                    getHandler().post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onError(e);
                        }
                    });
                }
            }
        });
    }
}




Java Source Code List

com.afollestad.cardsui.ApplicationTest.java
com.afollestad.cardsui.CardAdapter.java
com.afollestad.cardsui.CardBase.java
com.afollestad.cardsui.CardCenteredHeader.java
com.afollestad.cardsui.CardCompressed.java
com.afollestad.cardsui.CardCursorAdapter.java
com.afollestad.cardsui.CardHeader.java
com.afollestad.cardsui.CardListView.java
com.afollestad.cardsui.CardTheme.java
com.afollestad.cardsui.Card.java
com.afollestad.silk.ApplicationTest.java
com.afollestad.silk.SilkComparable.java
com.afollestad.silk.SilkCursorItem.java
com.afollestad.silk.Silk.java
com.afollestad.silk.activities.SilkDrawerActivity.java
com.afollestad.silk.adapters.ScrollStatePersister.java
com.afollestad.silk.adapters.SilkAdapter.java
com.afollestad.silk.adapters.SilkCursorAdapter.java
com.afollestad.silk.adapters.SilkSpinnerAdapter.java
com.afollestad.silk.dialogs.SilkDialog.java
com.afollestad.silk.fragments.feed.SilkCursorFeedFragment.java
com.afollestad.silk.fragments.feed.SilkFeedFragment.java
com.afollestad.silk.fragments.list.SilkCursorListFragment.java
com.afollestad.silk.fragments.list.SilkListFragment.java
com.afollestad.silk.http.SilkHttpBase.java
com.afollestad.silk.http.SilkHttpBody.java
com.afollestad.silk.http.SilkHttpCallback.java
com.afollestad.silk.http.SilkHttpClient.java
com.afollestad.silk.http.SilkHttpException.java
com.afollestad.silk.http.SilkHttpHeader.java
com.afollestad.silk.http.SilkHttpResponse.java
com.afollestad.silk.utilities.IOUtils.java
com.afollestad.silk.utilities.TimeUtils.java
com.afollestad.silk.views.ClickableToast.java
com.afollestad.silk.views.list.OnSilkScrollListener.java
com.afollestad.silk.views.list.SilkGridView.java
com.afollestad.silk.views.list.SilkListView.java
com.afollestad.silk.views.time.SilkDatePicker.java
it.gcaliendo.elytheme.Adw.java
it.gcaliendo.elytheme.ApplicationTest.java
it.gcaliendo.elytheme.DocksProvider.java
it.gcaliendo.elytheme.Docks.java
it.gcaliendo.elytheme.IconActivity.java
it.gcaliendo.elytheme.IconPack.java
it.gcaliendo.elytheme.IconsProvider.java
it.gcaliendo.elytheme.Icons.java
it.gcaliendo.elytheme.RequestActivity.java
it.gcaliendo.elytheme.ThemeActivity.java
it.gcaliendo.elytheme.Wallpaper.java
it.gcaliendo.elytheme.fragments.FragmentAbout.java
it.gcaliendo.elytheme.fragments.FragmentContact.java
it.gcaliendo.elytheme.fragments.FragmentExtras.java
it.gcaliendo.elytheme.fragments.FragmentTheme.java
it.gcaliendo.elytheme.helper.AppInfo.java
it.gcaliendo.elytheme.iconfragment.IconFragmentGames.java
it.gcaliendo.elytheme.iconfragment.IconFragmentLatest.java
it.gcaliendo.elytheme.iconfragment.IconFragmentMisc.java
it.gcaliendo.elytheme.iconfragment.IconFragmentPlay.java
it.gcaliendo.elytheme.iconfragment.IconFragmentSystem.java