0

我做了一个 HttpPost 到“ 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);

在后台执行时

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

logCat 显示此“org.apache.http.message.BasicHttpResponse@413f0a28”

我如何显示结果页面,从电脑访问时应该是这个“ https://portal.sibt.nsw.edu.au/std_Alert.asp ”。

4

3 回答 3

2

response.toString()将打印对象引用而不是内容。您想要的是读取响应内容并将其转换为String. 最简单的方法是使用EntityUtils.toString()类,这是一个示例:

HttpResponse response = client.execute(post);
String responseContent = EntityUtils.toString(response.getEntity());
Log.d("Response", responseContent );
于 2013-02-16T00:15:17.167 回答
0

我在下面的课程中这样做。你可以在你的项目中使用这个库,或者你可以复制代码。

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

以下是重要的方法:

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();
}

和来自另一个类(上面使用):

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) { }
    }
}
于 2013-02-16T00:14:19.117 回答
-1

要使 toString 不起作用,您需要从响应流中读取:

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

您可能需要增加缓冲区大小或以块为单位读取数据,但这正是您所需要的。

于 2013-02-16T00:05:15.107 回答