简体   繁体   English

进行HTTPS调用时出现NullPointerException

[英]NullPointerException when making HTTPS call

I've been scratching my head on this issue for the last few days now. 我过去几天一直在讨论这个问题。 I'm trying to POST data to a server which has an SSL certificate installed. 我正在尝试将数据发布到安装了SSL证书的服务器上。

I've been using the EasySSLSocketFactory and the EasyX509TrustManager classes found here ( http://code.google.com/p/transdroid/source/browse/trunk/src/org/xmlrpc/android/?r=103 ) in conjunction with the following code: 我一直在使用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;
}

But I seem to be getting the following back from my requests: 但我似乎从我的要求中得到了以下回复:

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 

Does anyone know what could be causing this? 有谁知道是什么原因引起的? I'm a bit stumped on this.. 我有点难过这个..

Edit: 编辑:

I think the issue lies here 我认为问题出在这里

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

The code seems to be passing a null string to the Class.forName() function, causing it to bail out.. But I'm not sure what's calling that and why. 代码似乎是将一个空字符串传递给Class.forName()函数,导致它纾困..但我不确定是什么调用它以及为什么。

Thanks, Oli 谢谢,奥利

After a long discussion the solution is 经过长时间的讨论,解决方案是

use IntelliJ IDEA ;-) 使用IntelliJ IDEA ;-)

Looks like Eclipse was causing the issue for me. 看起来Eclipse对我来说是个问题。 As gabe suggested, Intellij idea community edition compiles the code fine and doesn't cause the NullPointerException. 正如gabe所说,Intellij idea社区版编译代码很好并且不会导致NullPointerException。 I have no idea what could be causing it and no idea how to fix it. 我不知道是什么导致它,不知道如何解决它。 I'm just going to use a different IDE from now on. 我将从现在开始使用不同的IDE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM