0

I made an HttpPost to "https://portal.sibt.nsw.edu.au/Default.asp"

HttpClient client = new DefaultHttpClient();
String URL = (String) "https://portal.sibt.nsw.edu.au/Default.asp";
HttpPost post = new HttpPost(URL);

When executed in a the background

HttpResponse response = client.execute(post);
Log.d("Response", response.toString());

logCat displays this "org.apache.http.message.BasicHttpResponse@413f0a28"

how do i display the resulting page, which should be this "https://portal.sibt.nsw.edu.au/std_Alert.asp" when accessed from a pc.

Sabbib
  • 47
  • 2
  • 9

3 Answers3

2

response.toString() will print the object reference not the content. What you want is to read the response content and covert it to String. The simplest way to do this is using EntityUtils.toString() class, here is an example:

HttpResponse response = client.execute(post);
String responseContent = EntityUtils.toString(response.getEntity());
Log.d("Response", responseContent );
iTech
  • 18,192
  • 4
  • 57
  • 80
  • thanks that helped me see the code of the responding page in logCat, but how do i display the responding page in a new activity? that is my original question. i have the login creds. in the opening/main activity, after login button is pressed, the code is executed in the background, and a response is received in the main_activity.java. how do i take the responding page and display it in a new activity. if you wish to see the repsonding page i can lend you a id and password. – Sabbib Feb 16 '13 at 07:56
  • and if i have to use a webview how would i get the response and set the webview, i am not planning to use a webview if it looks just like how it would do on a browser, then users can just use the browser on the phone, however i am willing to do this initially, to get started. again if you wish to see the response page i can provide a login cred, please ask note:im new to android development – Sabbib Feb 16 '13 at 08:01
  • `WebView` will make the page look like the browser. See this question http://stackoverflow.com/questions/4543349/load-local-html-in-webview If you want to display `native` anrdoid interface, your website should be exposing sort of `APIs` that you can use to receive the information – iTech Feb 16 '13 at 16:59
0

I do this in the following class. You can use this library in your project or you can just copy the code.

https://github.com/aguynamedrich/beacon-utils/blob/master/Library/src/us/beacondigital/utils/StringUtils.java

Here are the important methods:

public static String readStream(HttpResponse response) {
    String data = null;
    try
    {
        data = readStream(response.getEntity().getContent());
    }
    catch(Exception ex) { }
    return data;
}


public static String readStream(InputStream in)
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder sb = new StringBuilder();
    String line = null;
    try
    {
        while((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
    }
    catch(Exception ex) { }
    finally
    {
        IOUtils.safeClose(in);
        IOUtils.safeClose(reader);
    }
    return sb.toString();
}

and from another class (used above):

https://github.com/aguynamedrich/beacon-utils/blob/master/Library/src/us/beacondigital/utils/IOUtils.java

public static void safeClose(Closeable closeable)
{
    if(closeable != null)
    {
        try
        {
            closeable.close();
        }
        catch (IOException e) { }
    }
}
Rich
  • 36,270
  • 31
  • 115
  • 154
-1

To toString wont work there you need to read from response stream:

byte[] buffer = new byte[1024];
int bytesRead = response.getEntity().getContent().read(buffer, 0, 1024);
String responseContent = new String(buffer, 0, bytesRead);

You may need to increase buffer size or to read data in blocks but this is what you need.

Velja Radenkovic
  • 716
  • 1
  • 6
  • 27