0

I'm trying to call a webservice with my application, but I get no error, the URL is the good one and return something (via the browser), but I get no content.

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();
    int lenght = (int) entity.getContentLength();
    is = entity.getContent();
} catch (Exception e) {
    Log.e("log_tag", "Error in http connection" + e.toString());
}

lenght is equal to -1 due to the empty response he receives

Does the response from the url need to be HTML ? Or anything I output can be grab by the HttpClient ?

2 Answers2

1

The response does not need to be HTML, but if the server side does not return a content-length header in the response, length will be negative.

Jeff Shelman
  • 416
  • 2
  • 4
1

The response does not necessarily need to be in HTML.

A negative value returned by getContentLength() means the content length is not returned by the server. It does not mean there is no content. It's possible to have content returned by the request, but still have a negative value returned by getContentLength().

You can still get the content returned by the request:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
entity.writeTo(baos);
String contentString = baos.toString();
craig65535
  • 3,439
  • 1
  • 23
  • 49
  • I added your 3 lines, and it fails at "entity.writeTo(baos);. The error is "java.lang.IllegalStateException: Content has been consumed". What does that means ? – Yann Boisclair-Roy Jul 26 '12 at 22:03