Search code examples
javaandroidillegalstateexception

Android: Exception "java.lang.IllegalStateException: Scheme 'https' not registered"


A user has sent me this stacktrace due to a force close:

java.lang.IllegalStateException: Scheme 'https' not registered.
at org.apache.http.conn.scheme.SchemeRegistry.getScheme(SchemeRegistry.java:80)
at org.apache.http.impl.conn.DefaultHttpRoutePlanner.determineRoute(DefaultHttpRoutePlanner.java:107)
at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:581)
at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:923)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:473)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.augustinianum.augustinianum.AlarmService.doWakefulWork(AlarmService.java:70)
at com.commonsware.cwac.wakeful.WakefulIntentService.onHandleIntent(WakefulIntentService.java:106)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.os.HandlerThread.run(HandlerThread.java:60)

Here's the most important code of the class that force closes, it's a WakefulIntentService by Commonsware, which is basically just a normal IntentService but this one automatically holds a wake-lock (don't think this has got anything to do with the exception, but just in case). This codes downloads the HTML code of a webpage:

private DefaultHttpClient createHttpClient() {
        HttpParams my_httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(my_httpParams, 3000);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        ThreadSafeClientConnManager multiThreadedConnectionManager = new ThreadSafeClientConnManager(my_httpParams, registry);
        DefaultHttpClient httpclient = new DefaultHttpClient(multiThreadedConnectionManager, my_httpParams);
        return httpclient;
    }


@Override
    protected void doWakefulWork(Intent arg0) {

        int mStatusCode = 0;
        String content = "";

        String url = "http://www.example.com/";

        DefaultHttpClient httpclient = createHttpClient();
        HttpGet httpget = new HttpGet(url);

        try {
            HttpResponse response = httpclient.execute(httpget);
            StatusLine statusLine = response.getStatusLine();
            mStatusCode = statusLine.getStatusCode();

            if (mStatusCode == 200){
                content = EntityUtils.toString(response.getEntity());
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Line 70 (where it crashes) is this line:

HttpResponse response = httpclient.execute(httpget);

In my manifest, I just declared it like this:

<service
    android:name=".AlarmService">
</service>

The weird thing is that this exception occurs not on all devices and not always when this code is excecuted. Most of the time, this code is just executed fine, but sometimes, this exceptions occurs.

Does anybody know what I can do about this?

Many many thanks in advance!


Solution

  • You do not need a ThreadSafeClientConnManager, as an IntentService only uses one thread. I would remove all that stuff, which would get rid of your custom SchemeRegistry, which may help you with your unregistered scheme.