【问题标题】:How to display the HttpResponse page in an android activity如何在 android 活动中显示 HttpResponse 页面
【发布时间】:2013-02-16 00:01:54
【问题描述】:

我向“https://portal.sibt.nsw.edu.au/Default.asp”发了一个 HttpPost

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”。

【问题讨论】:

    标签: android http-post httpresponse


    【解决方案1】:

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

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

    【讨论】:

    • 感谢帮助我在 logCat 中查看响应页面的代码,但是如何在新活动中显示响应页面?这是我最初的问题。我有登录凭据。在opening/main Activity中,按下登录按钮后,代码在后台执行,并在main_activity.java中收到响应。我如何获取响应页面并将其显示在新活动中。如果您想查看响应页面,我可以借给您一个 ID 和密码。
    • 如果我必须使用 webview 我将如何获得响应并设置 webview,我不打算使用 webview 如果它看起来就像在浏览器上一样,那么用户可以只使用手机上的浏览器,但我愿意一开始就这样做,开始。再次,如果您希望看到响应页面,我可以提供登录凭据,请询问注意:我是 android 开发新手
    • WebView 将使页面看起来像浏览器。看到这个问题stackoverflow.com/questions/4543349/load-local-html-in-webview 如果你想显示native anrdoid 界面,你的网站应该暴露某种APIs,你可以用它来接收信息
    【解决方案2】:

    我在下面的课程中这样做。您可以在项目中使用此库,也可以直接复制代码。

    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) { }
        }
    }
    

    【讨论】:

      【解决方案3】:

      toString 无法工作,您需要从响应流中读取:

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

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

      【讨论】:

      • 如果response 大于1024,这将对其进行代码修剪?
      猜你喜欢
      • 2019-04-23
      • 1970-01-01
      • 2016-04-14
      • 2013-06-08
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      相关资源
      最近更新 更多