NoSuchFieldError when trying to access REST service method via Retrofit and SSL
<p>I'm building a simple Android list view application which populates its list from the results of a REST service method hosted on a JBoss application server using SSL and a self-signed server certificate.</p>

<p>I'm using Android Studio 2.1.3 on a Windows 7 Professional 64-bit machine.</p>

<p>I'm been searching for how to use Retrofit to communicate via SSL to a REST service with self-signed certificates for a few days. I've found several examples, none of which work for me - I think due to my having the wrong versions of various dependencies, etc. The current code almost works, so I'm hoping with some help I can get it there.</p>

<p>The error I'm getting in the log is as follows:</p>

<pre><code>    java.lang.NoSuchFieldError: No static field METHODS of type Ljava/util/Set; in class Lcom/squareup/okhttp/internal/http/HttpMethod; or its superclasses (declaration of 'com.squareup.okhttp.internal.http.HttpMethod' appears in /data/app/com.mehconsulting.mehrestclient-1/base.apk)
at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.setRequestMethod(HttpURLConnectionImpl.java:550)
at com.squareup.okhttp.internal.huc.DelegatingHttpsURLConnection.setRequestMethod(DelegatingHttpsURLConnection.java:113)
at com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:25)
at retrofit.client.UrlConnectionClient.prepareRequest(UrlConnectionClient.java:50)
at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:37)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:818)
</code></pre>

<p>I'm have the following dependencies in my application module's build.gradle file:</p>

<pre><code>compile 'com.squareup.okio:okio:1.6.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.7.2'
</code></pre>

<p>Here is my service interface class:</p>

<pre><code>public interface MEHServiceInterface {
    @GET("/MEHService/rest/release/releases")
    void getReleases( Callback&lt;TitleReleases&gt; cb );
}
</code></pre>

<p>Here is my TitleReleases class:</p>

<pre><code>public class TitleReleases {
    List&lt;TitleRelease&gt; titleReleases;
}
</code></pre>

<p>Here is my TitleRelease class:</p>

<pre><code>public class TitleRelease {
    private Integer id;
    private String vin;
    private Integer state;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }
}
</code></pre>

<p>And finally, here is my MainActivity class:</p>

<pre><code>public class MainActivity extends ListActivity  implements Callback&lt;TitleReleases&gt; {

    ArrayAdapter&lt;String&gt; mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Certificate ca =  null;
        OkHttpClient okHttpClient = new OkHttpClient();

        try {
            // Load CAs from an InputStream
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            InputStream caInput = getApplicationContext().getResources().openRawResource(R.raw.stargate);
            try {
                ca = cf.generateCertificate(caInput);
                System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
            } finally {
                caInput.close();
            }
            } catch (Exception ex) {
                Log.e("MEH", "Unable to load the certificate: " + ex.getMessage());
        }

        // SSLContext sslContext = null;
        try {
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);

            // creating a TrustManager that trusts the CAs in our keyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);

            // creating an SSLSocketFactory that uses our TrustManager
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init( null, tmf.getTrustManagers(), null );
            /* okhttp version 3 way:
            okHttpClient = new OkHttpClient.Builder()
                    .sslSocketFactory(sslContext.getSocketFactory())
                    .build();
            */
            // okhttp version 2 way:
            okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
        } catch ( Exception ex ) {
            Log.e("MEH", "Unable to create the trust manager: " + ex.getMessage() );
        }

        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint("https://www.mehconsulting.com:8443")
                // .setClient( new OkClient( okHttpClient ) )
                .setClient(  new OkClient( okHttpClient )  )
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .build();

        MEHServiceInterface mehService = restAdapter.create( MEHServiceInterface.class );

        mehService.getReleases(this);
    }

    @Override
    public void failure( RetrofitError exception ) {
        Toast.makeText( this, exception.getMessage(), Toast.LENGTH_LONG).show();
        Log.e(getClass().getSimpleName(), "Exception from Retrofit request to MEHService", exception );
    }

    @Override
    public void success( TitleReleases releases, Response response ) {
        setListAdapter( new TitleReleasesAdapter( releases.titleReleases ) );
    }

    class TitleReleasesAdapter extends ArrayAdapter&lt;TitleRelease&gt; {
        TitleReleasesAdapter( List&lt;TitleRelease&gt; titleReleases ) {
            super(MainActivity.this, android.R.layout.simple_list_item_1, titleReleases );
        }
    }

}
</code></pre>

<p>I'm worried that I'm providing too much information, so if there is anything else you need to see, please let me know.</p>

<p>Thanks for any help.</p>