我正在尝试使用servlet执行get请求,但是我遇到Communication Error(1001) - 处理连接时出错。
我的代码非常简单:
Engine.getInstance().getRegisteredClients().clear();
Engine.getInstance().getRegisteredClients().add(new HttpsClientHelper(null));
ClientResource cr = new ClientResource(URI);
cr.setRequestEntityBuffering(true);
cr.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
SensorUpdaterApp.getInstance().GOOGLE_ACCOUNT_EMAIL,
SensorUpdaterApp.getInstance().GOOGLE_ACCOUNT_TOKEN);
DataTableResource resource = cr.wrap(DataTableResource.class);
Long newId = null;
try{
newId = resource.createDataset();
} catch(Exception e){ e.printStackTrace(); }
这是我在logcat中得到的堆栈跟踪:
08-16 13:21:24.221: I/org.restlet(11223): Starting the internal [HTTPS/1.1] client
08-16 13:21:24.311: D/dalvikvm(11223): GC_FOR_MALLOC freed 9361 objects / 434480 bytes in 59ms
08-16 13:21:24.391: D/dalvikvm(11223): GC_FOR_MALLOC freed 444 objects / 16384 bytes in 54ms
08-16 13:21:24.471: D/dalvikvm(11223): GC_FOR_MALLOC freed 505 objects / 18800 bytes in 53ms
08-16 13:21:24.561: D/dalvikvm(11223): GC_FOR_MALLOC freed 703 objects / 26368 bytes in 41ms
08-16 13:21:24.631: D/dalvikvm(11223): GC_FOR_MALLOC freed 870 objects / 32216 bytes in 41ms
08-16 13:21:24.911: D/dalvikvm(11223): GC_FOR_MALLOC freed 15029 objects / 968672 bytes in 46ms
08-16 13:21:25.241: I/org.restlet(11223): A recoverable error was detected (1001), attempting again in 2000 ms.
08-16 13:21:25.731: D/dalvikvm(11223): GC_FOR_MALLOC freed 24605 objects / 1514848 bytes in 60ms
08-16 13:21:27.531: I/org.restlet(11223): A recoverable error was detected (1001), attempting again in 2000 ms.
08-16 13:21:28.491: D/dalvikvm(11223): GC_FOR_MALLOC freed 33569 objects / 1502352 bytes in 78ms
08-16 13:21:29.821: W/System.err(11223): Communication Error (1001) - Error while processing a connection
08-16 13:21:29.821: W/System.err(11223): at org.restlet.resource.ClientResource.doError(ClientResource.java:611)
08-16 13:21:29.821: W/System.err(11223): at org.restlet.engine.resource.ClientInvocationHandler.invoke(ClientInvocationHandler.java:236)
08-16 13:21:29.821: W/System.err(11223): at $Proxy5.createDataset(Native Method)
08-16 13:21:29.821: W/System.err(11223): at com.sensorupdater2.activities.main.CreateNewDatasetAsyncTask.doInBackground(CreateNewDatasetAsyncTask.java:38)
08-16 13:21:29.821: W/System.err(11223): at com.sensorupdater2.activities.main.CreateNewDatasetAsyncTask.doInBackground(CreateNewDatasetAsyncTask.java:1)
08-16 13:21:29.821: W/System.err(11223): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-16 13:21:29.821: W/System.err(11223): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-16 13:21:29.831: W/System.err(11223): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-16 13:21:29.831: W/System.err(11223): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-16 13:21:29.831: W/System.err(11223): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-16 13:21:29.831: W/System.err(11223): at java.lang.Thread.run(Thread.java:1102)
08-16 13:21:29.831: W/System.err(11223): Caused by: java.lang.NullPointerException
08-16 13:21:29.831: W/System.err(11223): at org.apache.harmony.xnet.provider.jsse.ClientSessionContext$HostAndPort.hashCode(ClientSessionContext.java:214)
08-16 13:21:29.841: W/System.err(11223): at java.util.HashMap.put(HashMap.java:393)
08-16 13:21:29.841: W/System.err(11223): at org.apache.harmony.xnet.provider.jsse.ClientSessionContext.putSession(ClientSessionContext.java:190)
08-16 13:21:29.841: W/System.err(11223): at org.apache.harmony.xnet.provider.jsse.ClientHandshakeImpl.unwrap(ClientHandshakeImpl.java:308)
08-16 13:21:29.841: W/System.err(11223): at org.apache.harmony.xnet.provider.jsse.SSLRecordProtocol.unwrap(SSLRecordProtocol.java:413)
08-16 13:21:29.841: W/System.err(11223): at org.apache.harmony.xnet.provider.jsse.SSLEngineImpl.unwrap(SSLEngineImpl.java:478)
08-16 13:21:29.841: W/System.err(11223): at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:383)
08-16 13:21:29.841: W/System.err(11223): at org.restlet.ext.ssl.internal.ReadableSslChannel.onDrain(ReadableSslChannel.java:114)
08-16 13:21:29.841: W/System.err(11223): at org.restlet.engine.io.Buffer.process(Buffer.java:553)
08-16 13:21:29.841: W/System.err(11223): at org.restlet.engine.io.Buffer.process(Buffer.java:668)
08-16 13:21:29.841: W/System.err(11223): at org.restlet.engine.io.ReadableBufferedChannel.read(ReadableBufferedChannel.java:172)
08-16 13:21:29.841: W/System.err(11223): at org.restlet.engine.io.Buffer.fill(Buffer.java:391)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.InboundWay.onFill(InboundWay.java:278)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.io.Buffer.process(Buffer.java:597)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.io.Buffer.process(Buffer.java:668)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.Way.processIoBuffer(Way.java:477)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.InboundWay.processIoBuffer(InboundWay.java:353)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.Way.onSelected(Way.java:430)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.ConnectionController.controlConnections(ConnectionController.java:105)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.ConnectionController.doRun(ConnectionController.java:156)
08-16 13:21:29.851: W/System.err(11223): at org.restlet.engine.connector.Controller.run(Controller.java:158)
08-16 13:21:29.851: W/System.err(11223): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
08-16 13:21:29.851: W/System.err(11223): ... 5 more
08-16 13:21:31.181: D/dalvikvm(11223): GC_FOR_MALLOC freed 31659 objects / 1496144 bytes in 101ms
08-16 13:21:31.181: I/org.restlet(11223): Stopping the internal[HTTPS/1.1] client
在我的libs目录中,我有:
我用其他客户端测试了服务器,它似乎正常工作
有什么想法吗?
答案 0 :(得分:2)
我发现只导入org.restlet.ext.net.HttpClientHelper
同时使HTTPS和HTTP工作,所以你可能想尝试一下(但要小心 - 这种行为会根据导入的HttpClientHelper
而改变。)
如果您故意想要使用org.restlet.ext.ssl.HttpsClientHelper
,那么您还需要导入org.jsslutils_1.0库。如果你没有,那么连接只会永远超时(至少这是我在Android上使用Restlet 2.1.0进行测试时看到的)。
Restlet 2.1.0 download中的readme.txt显示了每个JAR的依赖关系。 org.restlet.ext.ssl依赖于jsslutils:
org.restlet.ext.ssl(Restlet Extension - SSL)
- org.jsslutils_1.0
仅供参考,Restlet下载还包括所有依赖库,因此您可以从那里获取它们。
以下是我在测试时发现的其他一些杂项:
确保导入并使用正确的连接器(当我导入`HttpClientHelper Eclipse时,我提供了4个不同的连接器)。 Restlet wiki连接器页面列出了可用的服务器和客户端连接器:
http://wiki.restlet.org/docs_1.1/13-restlet/27-restlet/37-restlet.html
当我尝试org.restlet.ext.httpclient.HttpClientHelper
时,我认为我不需要为HTTP连接器导入所有依赖项(可能是因为我只使用客户端部分)。
确保所有JAR都来自相同的Restlet版本,否则您可能会遇到奇怪的错误。
如果您只导入org.restlet.ext.httpclient.HttpClientHelper
连接器而不是HTTPS连接器,则会看到java.lang.IllegalStateException: Scheme 'https' not registered
。