1

I'm trying to use HTTPClient to get a page, but it seems to return gibberish (I think binary).

Here's my code:

public Boolean getPage(String url, String referer) {
    httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    httpClient.getParams().setParameter("http.socket.timeout", new Integer(1000));
    httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");

    HttpGet httpGet = new HttpGet(url);
    response = null;

    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3");
    httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    httpGet.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    httpGet.setHeader("Accept-Encoding", "gzip,deflate");
    httpGet.setHeader("Referer", referer);

    int tryNumber = 0;

    while(tryNumber<5){
        tryNumber++;
        try {
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            ret = httpClient.execute(httpGet,responseHandler).toString();
            Log.v("Info:", ret);
        }
        catch(Exception e) {
            error = e;
            break;
        }
    }
}

Can anyone point out where I'm going wrong?

I'm trying to get the content of this page: http://hosh.me.uk/test.php

See screenshot: http://img.ctrlv.in/4ecd69c40a590.jpg

Hosh

GManz
  • 1,548
  • 2
  • 21
  • 42
  • 1
    Not that related, but looking at the picture you posted, I have to say - Start class name with an UPPER CASE letter (ChapterList.java and not chapterList.java) – MByD Nov 23 '11 at 21:50
  • This is not really my code, I normally do do that in Java, but this is just a svn checkout from another project, which has been dead, and I'm trying to improve on it and fix it basically. Cheers for the advice though, am still quite a beginner lol – GManz Nov 23 '11 at 21:52
  • 2
    Why do you have ISO-8859-1 in your Accept-Charset while you seem to favor UTF-8. Also if you Accept-Encoding "gzip,deflate", don't you need to handle with decompression, as shown here: http://stackoverflow.com/questions/1573391/android-http-communication-should-use-accept-encoding-gzip ? – David Dossot Nov 23 '11 at 21:53

1 Answers1

5

Just a guess, but could it be that the response is compressed? Maybe you should remove the following line so your response will be plain text.

httpGet.setHeader("Accept-Encoding", "gzip,deflate");
Matt Cofer
  • 2,972
  • 1
  • 20
  • 18
  • 1
    How the hell did I not see that?! :( Thanks for that! Worked! I'll accept the answer as soon as I can. – GManz Nov 23 '11 at 21:53
  • 1
    You're welcome. Also notice David Dossot's comment on your question too. He links to another question on here that shows you how to decompress your response. The link also claims you actually should use compressed communication. – Matt Cofer Nov 23 '11 at 21:56