我正在使用DefaultHttpClient
的Android应用程序上工作。在某些设备中,当我与客户端执行请求时,应用程序崩溃。例外:
E/ExceptionHandler: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.ClassCastException: $Proxy1 cannot be cast to org.apache.http.message.BasicHttpResponse
开始调试时,我注意到我正在使用的DefaultHttpClient
在应用程序崩溃的那些设备中具有不同的属性。我认为这可能是一个问题,因为DefaultHttpClient
已过时,因此我正在尝试更改HttpClient。
我当前对DefaultHttpClient
的实现是下一个:
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
return new DefaultHttpClient(ccm, params);
我需要将DefaultHttpClient
更改为HttpClientBuilder.create().build()
。如何将ClientConnectionManager
和HttpParams
设置为新的HttpClient
?
还有其他方法可以将当前属性设置为新的HttpClient
吗?
可能是解决导致应用崩溃的另一种方法吗?