创建 DefaultHttpClient 时 HttpClient 崩溃

发布于 2024-11-14 02:18:35 字数 1191 浏览 3 评论 0原文

我一直在尝试(失败)使用 HttpClient 库将 http 请求发送到 Web 服务我刚刚做了一个简单的类来测试 get 请求...

DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://localhost/mywebservice/1");
        try {
            HttpResponse response = client.execute(get);
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch(IOException e) {
e.printStackTrace();
    }

我的问题是它没有超过第一行,它抛出以下运行时异常...

Exception in thread "main" java.lang.RuntimeException: Stub!
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:5)
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:7)
    at Main.main(Main.java:20)

而且我在任何地方都找不到任何在线解决方案,因为一切似乎都在说这是将其与 android sdk 一起使用并且没有运行模拟器的问题,但我没有使用与 android sdk 捆绑的包。或者它与运行单元测试有关,但这只是类中的一个简单的主要方法。

任何帮助都会很棒,因为我一整天都在为此苦苦挣扎,

谢谢

Ive been trying(failing) to use the HttpClient library to send http requests to a webservice and ive just made a simply class to test out a get request...

DefaultHttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://localhost/mywebservice/1");
        try {
            HttpResponse response = client.execute(get);
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch(IOException e) {
e.printStackTrace();
    }

My problem is it doesnt get past the 1st line, it throws the following runtime exception...

Exception in thread "main" java.lang.RuntimeException: Stub!
    at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:5)
    at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:7)
    at Main.main(Main.java:20)

And I cant find any solutions online anywhere because everything seems to either say its a problem of using it with the android sdk and not having the emulator running but im not using the package bundled with the android sdk. Or its to do with running unit tests but this is just a simple main method in a class.

Any help would be great because ive been struggling with this all day

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

⊕婉儿 2024-11-21 02:18:35

遇到了同样的问题,来自 lenbloy 的链接有所帮助(谢谢!)。以下是要做的事情(在 Eclipse 中):

  1. 右键单击​​您的项目。
  2. 构建路径->配置构建路径...
  3. 转到“订购和导出”选项卡。
  4. 选择“httpclient-4.1.3.jar”(或您可能使用的任何其他版本)
  5. 单击右侧的“顶部”按钮。

Had the same problem, and the link from lenbloy helped (thanks!). Here is what to do (in eclipse):

  1. Right-click your project.
  2. Build Path -> Configure Build Path...
  3. Go to the "Order and Export" tab.
  4. Select "httpclient-4.1.3.jar" (or any other version you might use)
  5. Click the "Top" button on the right.
遗失的美好 2024-11-21 02:18:35

看看这个问题,它描述了“存根!”方法。

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

总之:

这是 Android 告诉您无法在该平台上运行该单元测试的方式。涉及与 Android 平台(例如本例中的网络)交互的单元测试需要在实际的 Android 设备或功能正常的 Android 模拟器上运行。

显然,您正在针对 Android SDK 中的存根类运行单元测试。

Look at this Question that describes what "Stub!" means.

Apache DefaultHttpClient invocation results in “java.lang.RuntimeException: Stub!”

In summary:

This is Android's way of telling you that you cannot run that unit test on that platform. Unit tests that involve interacting with the Android platform (e.g. the network in this case) need to be run on an actual Android device or a functioning Android emulator.

Apparently, you are running the unit tests against the stub classes in the Android SDK.

π浅易 2024-11-21 02:18:35

诡异的。 没有帮助吗?

其他建议:为了确保您没有使用 android lib,请使用 VM -verbose 选项运行代码,(java -verbose Myclass ;或者将开关添加到Eclipse 运行配置选项卡中的“VM 参数”)并查看从哪个 jar 加载 AbstractHttpClient。

Weird. Doesn't this help?

Other suggestion: to be sure you are not using the android lib, run the code with the VM -verbose option, (java -verbose Myclass ; or add the switch to the 'VM arguments' in the Eclipse run-configuration tab) and see from which jar is the AbstractHttpClient loading.

〃温暖了心ぐ 2024-11-21 02:18:35

在版本 28 之后,将其包含在您的清单文件中:

HttpClient 遗留库已被弃用。

After version 28, include this in your Manifest file:

HttpClient legacy library has been deprecated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文