Android: java.lang.IllegalStateException: Scheme 'http' 未注册 - HTTPS 调用错误

标签 android http exception https url-scheme

我有以下代码,用于检查与特定服务器的二级互联网连接。

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
                        SchemeRegistry registry = new SchemeRegistry();
                        SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
                        socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
                        registry.register(new Scheme("https", socketFactory, 8443));

                        DefaultHttpClient client = new DefaultHttpClient();

                        SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
                        DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());


                        HttpParams httpParameters = new BasicHttpParams();
                        HttpConnectionParams.setConnectionTimeout(httpParameters, Constants.HTTP_CONNECTION_TIMEOUT);

                        HttpConnectionParams.setSoTimeout(httpParameters, Constants.HTTP_CONNECTION_TIMEOUT);

                        HttpGet httpget = new HttpGet(Utility.getCloudUrl());

                        try {
                            HttpResponse response = httpClient.execute(httpget);

                            BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                            StringBuffer sb = new StringBuffer("");
                            String l = new String();
                            while ((l = in.readLine()) !=null){
                                sb.append(l);
                            }
                            in.close();
                            String data = sb.toString();
                            if (data.length() > 0){

                                synchronized (lock) {
                                    isInternetAvialbale = true;
                                }
                                listener.onInternetReachable();

                            }else{


                                synchronized (lock) {
                                    isInternetAvialbale = false;
                                }
                                listener.onInternetNotReachable();
                            }
                        } catch (IOException e) {


                            synchronized (lock) {
                                isInternetAvialbale = false;
                            }
                            listener.onInternetNotReachable();
                            return;
                        }

在 Wifi 网络上一切正常。但是当这段代码在 3g 网络上执行时,出现以下异常:

java.lang.IllegalStateException: Scheme 'http' not registered.
       at org.apache.http.conn.scheme.SchemeRegistry.getScheme(SchemeRegistry.java:80)
       at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:126)
       at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
       at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
       at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
       at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
       at com.day.demo.DayApplication$3.run(DayApplication.java:340)
       at java.lang.Thread.run(Thread.java:841)

我确实搜索了几个类似的问题,但无法理解。 有人可以帮我解决这个问题吗?

最佳答案

如果您使用组织的 WiFi 连接到互联网,请确保您可以访问互联网(而不仅仅是连接到互联网)。如果 WiFi 访问需要登录,请先登录,然后继续 HTTP/HTTPS 请求。

关于Android: java.lang.IllegalStateException: Scheme 'http' 未注册 - HTTPS 调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29340320/

相关文章:

java - Android SDK 无法使用 'find_java.exe' 的问题

python - 如何避免 HTTP 错误 429(请求过多)python

java - 如何在Session Beans中使用自定义异常?

PHP 异常 - 我的想法正确吗?

android - 是否可以本地化 CalendarView 或 DatePicker?

java - 如何在 android 中拉伸(stretch) GridView 的行,其中 gridview 包含按钮

android - 如何在没有外部应用程序的情况下打开/显示文档(.pdf、.doc)?

rest - 在 REST API 中使 PUT 创建请求幂等

apache - 是否可以将端口 80 用于 HTTP 和 Web 套接字流量?

java - 断言(假)与 RuntimeException?