开发者

HttpClient crashes on DefaultHttpClient creation

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 thr开发者_Go百科ows 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


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.


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.


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.


After version 28, include this in your Manifest file:

HttpClient legacy library has been deprecated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜