1

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!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Xander
  • 5,487
  • 14
  • 49
  • 77

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your answer, but could you be a little bit clearer what excactly I should remove? – Xander Mar 03 '13 at 13:43
  • @Merlin: `createHttpClient()` should return `new DefaultHttpClient();`. No constructor parameters, no rest of the stuff. – CommonsWare Mar 03 '13 at 13:51
  • Oh okay, I get it. I'll give that a try. Thanks! Alternitively, would it also be possible to add and extra catchblock `}catch(IllegalStateException e){`? – Xander Mar 03 '13 at 13:53
  • @Merlin: You could catch it, but I don't know what you'd do then to recover. – CommonsWare Mar 03 '13 at 13:59
  • Okay. Thank you very much. I'm going to try this and mark your answer as accepted when everything's working. Many thanks! – Xander Mar 03 '13 at 14:04
  • Unfortunately, your suggestion didn't work out, so I caught the exception. At least it doesn't crash now. – Xander Mar 21 '13 at 16:42
0

I also faced such issue, I got the solution for this, the simple solution for this check your proxy setting, this happen because of the proxy set for the network

Simply remove the proxy and try :) It will works for me

Vivek Hande
  • 929
  • 9
  • 11