进行HTTPS调用时出现NullPointerException

时间:2011-10-12 14:40:05

标签: java android https httpclient x509certificate

我过去几天一直在讨论这个问题。我正在尝试将数据发布到安装了SSL证书的服务器上。

我一直在使用EasySSLSocketFactory和这里的EasyX509TrustManager类(http://code.google.com/p/transdroid/source/browse/trunk/src/org/xmlrpc/android/?r=103)以及以下代码:

StringEntity se = null;
try {
    se = new StringEntity(tripArray.toString());
} catch (UnsupportedEncodingException e) { }

SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

HttpParams params = new BasicHttpParams();
params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);
this.httpClient = new DefaultHttpClient(cm, params); 

HttpPost httpPost = new HttpPost("https://xxx");
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

ResponseHandler responseHandler = new BasicResponseHandler();
try {
    String response = this.httpClient.execute(httpPost, responseHandler).toString();
} catch (ClientProtocolException e) {
    tripFail = true;
} catch (IOException e) {
    tripFail = true;
}

但我似乎从我的请求中得到了以下回复:

Thread [<10> Timer-1] (Suspended (exception NullPointerException))  
    Class.forName(String, boolean, ClassLoader) line: 234   
    Class.forName(String) line: 181 
    X509Certificate.<clinit>() line: 60 
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).verifyCertificateChain(byte[][], String) line: 658  
    NativeCrypto.SSL_do_handshake(int, FileDescriptor, NativeCrypto$SSLHandshakeCallbacks, int, boolean) line: not available [native method]    
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).startHandshake(boolean) line: 474   
    OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl) line: 750    
    OpenSSLSocketImplWrapper(OpenSSLSocketImpl).getInputStream() line: 692  
    SocketInputBuffer.<init>(Socket, int, HttpParams) line: 98  
    DefaultClientConnection(SocketHttpClientConnection).createSessionInputBuffer(Socket, int, HttpParams) line: 83  
    DefaultClientConnection.createSessionInputBuffer(Socket, int, HttpParams) line: 170 
    DefaultClientConnection(SocketHttpClientConnection).bind(Socket, HttpParams) line: 106  
    DefaultClientConnection.openCompleted(boolean, HttpParams) line: 129    
    DefaultClientConnectionOperator.openConnection(OperatedClientConnection, HttpHost, InetAddress, HttpContext, HttpParams) line: 171  
    SingleClientConnManager$PoolEntry(AbstractPoolEntry).open(HttpRoute, HttpContext, HttpParams) line: 164 
    SingleClientConnManager$ConnAdapter(AbstractPooledConnAdapter).open(HttpRoute, HttpContext, HttpParams) line: 119   
    DefaultRequestDirector.execute(HttpHost, HttpRequest, HttpContext) line: 359    
    DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, HttpContext) line: 555 
    DefaultHttpClient(AbstractHttpClient).execute(HttpHost, HttpRequest, ResponseHandler, HttpContext) line: 653    
    DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, ResponseHandler, HttpContext) line: 627   
    DefaultHttpClient(AbstractHttpClient).execute(HttpUriRequest, ResponseHandler) line: 616    
    DataSender.sendData() line: 151 
    DataSender$1.run() line: 202    
    Timer$TimerImpl.run() line: 284 

有谁知道造成这种情况的原因是什么?我有点难过......

编辑:

我认为问题出在这里

Class.forName(String, boolean, ClassLoader) line: 234   
    Class.forName(String) line: 181 

代码似乎是将一个空字符串传递给Class.forName()函数,导致它纾困..但我不确定是什么调用它以及为什么。

谢谢, 奥利

2 个答案:

答案 0 :(得分:0)

经过长时间的讨论,解决方案是

使用IntelliJ IDEA; - )

答案 1 :(得分:0)

看起来Eclipse正在为我造成这个问题。正如gabe所说,Intellij idea社区版编译代码很好并且不会导致NullPointerException。我不知道是什么导致它,不知道如何解决它。我将从现在开始使用不同的IDE。