Skip to content

Configuring RoboZombie

Lahiru Sahan Jayasinghe edited this page Jan 5, 2014 · 3 revisions

Every endpoint uses an existing configuration for request execution. Such a configuration usually pertains to the settings applied on Apache HC. Familiarization with Apache HC should be a prerequisite for building a custom configuration.


The default configuration employs a [DefaultHttpClient](http://developer.android.com/reference/org/apache/http/impl/client/DefaultHttpClient.html) which uses a [ThreadSafeClientConnManager](http://developer.android.com/reference/org/apache/http/impl/conn/tsccm/ThreadSafeClientConnManager.html) with the following parameters.
Parameter Configuration
Redirecting ENABLED
Connection Timeout 30 seconds
Socket Timeout 30 seconds
Socket Buffer Size 12000 bytes
User-Agent System.getProperty("http.agent")

Additionally, two schemes are registered.
Scheme Port Socket Factory
HTTP 80 PlainSocketFactory
HTTPS 443 SSLSocketFactory

A configuration can be tailored for a specific endpoint by creating an implementation of `Zombie.Configuration`. Currently the only configurable property is the `HttpClient`. Once a configuration is created, it can be applied on an endpoint using the `@Config` annotation by specifying the `Class` of the implementation.
import android.net.http.AndroidHttpClient;

public class GitHubConfig extends Zombie.Configuration {

    @Override
    public HttpClient httpClient() {
		
        HttpClient httpClient = AndroidHttpClient.newInstance("MyGitHubApp-v1");

        HttpParams params = httpClient.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
        HttpConnectionParams.setSoTimeout(params, 10 * 1000);
					
        return httpClient;
    }
}

@Config(GitHubConfig.class)
@Endpoint("https://api.github.com")
public interface GitHubEndpoint {

    ...
}

Demonstrates the use of AndroidHttpClient instead of a DefaultHttpClient.

You can tweak the preconfigured DefaultHttpClient as well.

public class TweakedConfig extends Zombie.Configuration {

    @Override
    public HttpClient httpClient() {
		
        HttpClient httpClient = super.httpClient();
        HttpProtocolParams.setUserAgent(httpClient.getParams(), "MyGitHubApp-v1");
					
        return httpClient;
    }
}