1

I run SAME code on my PC (windows 7 64 bit, Eclipse, Java) and on a Android Virtual Machine and I get different results.

Its a small programm that shoult print me the HttpResponse fully as String.

Results on PC:

HTTP/1.1 405 Method Not Allowed [Allow: GET, HEAD, Date: Thu, 03 Nov 2011 17:57:22 GMT, Content-Type: text/html; charset=UTF-8, Server: gws, Content-Length: 11816, X-XSS-Protection: 1; mode=block, X-Frame-Options: SAMEORIGIN]

Result on Android:

org.apache.http.message.BasicHttpResponse@44ec9da8

The code I ran on PC was:

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "http://www.google.com");

    try {
        HttpResponse response = httpclient.execute(httppost);
        System.out.println(response.toString());

And on Android:

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "http://www.google.com");

    try {
        HttpResponse response = httpclient.execute(httppost);
        return response.toString();

Im asking becasue I wrote a App that would work on PC - works with the HttpResponse content - but on android it seems that is has NO content at all...

Could anyone please explain me this? Or tell me how to get the response content (headers) as String on android?

Oliver Goossens
  • 1,903
  • 3
  • 20
  • 26
  • are you using the same version of httpClient on both? Andoid's httpClient is some 4beta... – Ognyan Nov 03 '11 at 18:09
  • 1
    I would highly recommend *not* to use the output of a `toString()` method for any actual program behavior -- it's just not reliable. Rather get specific pieces from `HttpResponse` or return it as a whole. – Philipp Reichart Nov 03 '11 at 18:36

2 Answers2

2

This is because HttpResponse.toString() is implemented differently on Android and on PC (different jvm and framework components implementations!). To get HttpHeaders correctly I guess you need to use something like:

http://developer.android.com/reference/org/apache/http/HttpMessage.html#getAllHeaders%28%29

Anyhow see javadoc for reference on this.

pkk
  • 3,691
  • 2
  • 21
  • 29
  • This has nothing to do with JVM implementations, but with the implementation of HttpClient, still you are correct about the different `toString()` methods. – Philipp Reichart Nov 03 '11 at 18:17
  • 1
    However from my knowledge, HttpClient is a part of standard android framework, and might have been implemented differently from the "regular" apache http components. If you really need to verify this, have a look at android source code, available at http://stackoverflow.com/questions/449763/where-can-i-browse-android-source-code-on-line – pkk Nov 03 '11 at 18:21
  • I fully agree with you on the different implementations, I'm just nitpicking on the terminology: Libraries/framework classes aren't part of the VM and there's no JVM at all on Android, there's only the Dalvik VM :) – Philipp Reichart Nov 03 '11 at 18:32
  • You're absolutely right :) jvm != dvm nor, the http components are part of it. Thanks for pointing this out - hope my quick edit made this clear. – pkk Nov 03 '11 at 18:42
2

You don't actually run the same code.

Android's BasicHttpResponse currently has no toString() method at all and goes on to invoke java.lang.Object.toString() giving you fully.qualified.class.name@hashcode.

Your desktop version of HttpClient most likely has a toString() similar to this one from HttpClient 4.1.3:

public String toString() {
    return this.statusline + " " + this.headergroup;
}

Using toString() for any non-debugging/logging purposes isn't good practice -- you should extract the right information using actual get methods or just return the HttpResponse itself.

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65