DefaultHttpClient.execute抛出ClassCastException

时间:2019-04-17 22:05:21

标签: android apache-httpclient-4.x

我正在使用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()。如何将ClientConnectionManagerHttpParams设置为新的HttpClient

还有其他方法可以将当前属性设置为新的HttpClient吗?

可能是解决导致应用崩溃的另一种方法吗?

0 个答案:

没有答案