Apache DefaultHttpClient调用导致“java.lang.RuntimeException:Stub!”

我正在将我的脚趾浸入Android开发中。 我有一个项目将与RESTful资源接口,我正在试图找出如何通过HTTP上的params进行基本GET。 从我读过的所有内容来看,共识似乎都支持HTTPClient优于HttpURLConnection。

我编写了一个包装类,其中包含一个方法,该方法负责实例化密钥对象以使用HTTPClient发出请求:

public String get() { String responseString = null; HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(); try { get.setURI(new URI(this.baseURL())); } catch (URISyntaxException e1) { e1.printStackTrace(); } HttpResponse response; try { response = client.execute(get); responseString = readResponse(response.getEntity()); if(response != null) { System.out.println(responseString); } } catch(ClientProtocolException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return responseString; 

}

HttpClient client = new DefaultHttpClient(); 抛出以下exception:

 java.lang.RuntimeException: Stub! at org.apache.http.impl.client.AbstractHttpClient.(AbstractHttpClient.java:5) at org.apache.http.impl.client.DefaultHttpClient.(DefaultHttpClient.java:7) at org.rcindustries.appmap.RestClient.get(RestClient.java:54) at org.rcindustries.appmap.test.functional.RestClientTest.shouldReturnSomeJSon(RestClientTest.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我见过的每个HttpClient示例都使用类似的结构来执行GET和POST。 与Android SDK捆绑在一起的Apache Commons库是否与标准库明显不同?

我认为这是Android告诉你不能在该平台上运行unit testing的方式。 涉及与Android平台交互的unit testing(例如,在这种情况下为网络)需要在实际的Android设备或正常运行的Android模拟器上运行。

显然,您正在针对Android SDK中的存根类运行unit testing。

对于任何可能感兴趣的人,我遇到了类似的问题 – 虽然我没有使用HttpClient获取DateUtils存根错误。

斯蒂芬似乎绝对正确 – 属于Android平台的类需要启动和运行模拟器: http : //simpleprogrammer.com/2010/07/27/the-best-way-to-unit-test-in-安卓/

以上链接的快速摘要:

事实上,当你调用它们时,所有方法都被删除,以便在消息“Stub!”中抛出exception。 真可爱。

真正的android.jar实现存在于模拟器或真正的Android设备上。

因此,您可以进行unit testing……

  • 在模拟器上 – 获得完整的Android平台,“真正的”android环境等(但没有常见Javaunit testing工具的强大function – 例如JMock)

要么

  • 使用JVM – 速度更快,可以使用像JMock等unit testing助手,但是你无法测试任何依赖于Android平台的东西。

在Android SDK 23中使用Proguard和com.apache.http.legacy库时会发生这种情况。

在我将它添加到我的Proguard配置后它工作了:

 -keep class org.apache.http.** { *; } -keep class org.apache.commons.codec.** { *; } -keep class org.apache.commons.logging.** { *; } -keep class android.net.compatibility.** { *; } -keep class android.net.http.** { *; } -keep class com.android.internal.http.multipart.** { *; } -dontwarn org.apache.http.** -dontwarn android.webkit.** 

这允许系统Apache实现正确地覆盖编译到应用程序中的存根。

这个线程有点陈旧,但对于不知道的人, Robolectric解决了这个问题进行测试。