Skip to content
Ludovic ROLAND edited this page Mar 23, 2018 · 6 revisions

Download License: MIT TeamCity status

Using the okhttp implementation in your project

Add the following in your app module build.gradle file:

dependencies
{ 
  implementation ("com.smartnsoft:okhttpwebservicecaller:${latest.version}")
}

Create a simple WebServiceCaller client for your app

The minimal code

In order to create a WebServiceCaller client for your Android project, just extends from the OkHttpClientWebServiceCaller and implement the following methods:

  • getUrlEncoding: this method returns the charset to use for encoding the URI parameters ;
  • getContentEncoding: this method returns the charset to use for decoding the web service requests content.

You also have to the create a constructor and call the super one with 3 parameters :

  • readTimeOutInMilliseconds
  • connectTimeOutInMilliseconds
  • acceptGzip

In order to optimize your application, we recommand to implement the singleton pattern. For example:

public final class ExempleServices
    extends OkHttpClientWebServiceCaller
{

  private static volatile ExempleServices instance;

  // We accept the "out-of-order writes" case
  public static ExempleServices getInstance()
  {
    if (instance == null)
    {
      synchronized (ExempleServices.class)
      {
        if (instance == null)
        {
          instance = new ExempleServices();
        }
      }
    }

    return instance;
  }

  private ExempleServices()
  {
    super(5_000, 5_000, true);
  }

  @Override
  protected String getUrlEncoding()
  {
    return Encoding.UTF_8.toString();
  }

  @Override
  protected String getContentEncoding()
  {
    return Encoding.ISO_8859_1.toString();
  }

}

Other features

By default, the component will create his own instance of the Square okhttp client, but you can work with your own overriding the computeHttpClient method.

@Override
protected Builder computeHttpClient()
{
  return super.computeHttpClient();
}

By default, the component will recreate a new okhttp client for each request but in order to optimize the performance of your app, you can indicate to the component to reuse the same http client with the ReuseOkHttpClient annotation:

@ReuseOkHttpClient
public final class ExempleServices
    extends OkHttpClientWebServiceCaller
{
  //
}

How to a request ?

With no cache

//TODO

With cache

//TODO

How to use Jackson ?

//TODO

Clone this wiki locally