【问题标题】:How do I use the command: HttpEntity?如何使用命令:HttpEntity?
【发布时间】:2012-03-13 07:07:04
【问题描述】:

我希望通过这个问题来解决我的长期问题,并希望你们能提供帮助,但首先;近 3 周以来,我一直在连接 HTTPS 自签名证书服务器时遇到问题。尽管这里有多种解决方案,但我似乎无法解决我的问题。可能我不知道如何正确使用它或者没有一些文件或导入正确的库。

我遇到了一些网站,要求我从我尝试连接的 https 网站下载证书,以及我下载的时间。在使用我创建的证书或密钥库之前,我必须执行一些步骤。我从这个网站得到了这个解决方案:

Android: Trusting SSL certificates

// Instantiate the custom HttpClient
DefaultHttpClient client = new MyHttpClient(getApplicationContext());
HttpGet get = new HttpGet("https://www.mydomain.ch/rest/contacts/23");
// Execute the GET call and obtain the response
HttpResponse getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();

如上所述,在最后一行之后,我有一个问题。我该如何处理 responseEntity?如果我想在 WebView 上显示 https 网站,如何使用它?一些帮助和解释会很好:)

【问题讨论】:

    标签: android https android-2.3-gingerbread


    【解决方案1】:

    如果您想要来自HttpEntity 的内容,正确 方式是否 包括调用HttpEntity#getContent() 来检索流并执行大量已经在Android SDK。

    试试这个。

    // Execute the GET call and obtain the response
    HttpResponse getResponse = client.execute(get);
    HttpEntity responseEntity = getResponse.getEntity();
    
    // Retrieve a String from the response entity
    String content = EntityUtils.toString(responseEntity);
    
    // Now content will contain whatever the server responded with and you
    // can pass it to your WebView using #loadDataWithBaseURL
    

    在显示content 时考虑使用WebView#loadDataWithBaseURL - 它的表现要好得多。

    【讨论】:

    • 噢,我是盲人。我没有看到这个答案。无论如何,我要结束我的工作,回家后会试试这个。请检查这个网站,因为我肯定会回复它是否有效!
    • 我设法运行该应用程序而没有任何运行时错误,但我没有得到任何输出。经过一些繁琐的调试。 getResponse = client.execute(get) 返回一个空值。我明白为什么我可能会得到一个空返回。您提供的代码要求我使用 try-catch 方法。结果看起来像这样:try { getResponse = client.execute(get); HttpEntity responseEntity = getResponse.getEntity(); content = EntityUtils.toString(responseEntity); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
    • 嗨,我已经有一段时间没有做这个应用程序了。我试过你的String content = EntityUtils.toString(responseEntity); 工作得很好,但它有点返回 HTML 代码而不是 URL 链接。我是不是做错了什么?
    • 这就是它应该做的。来自服务器的 HTML 响应中是否有一个 URL 链接,您需要稍后获取并使用它 - 还是用于处理重定向?
    • 对不起!现在解决了这个问题,您对 loadDataWithBaseURL 的看法是正确的。我只是不知道如何正确使用它!在本站其他人的帮助下设法解决了! :)
    【解决方案2】:

    您需要调用 responseEntity.getContent() 以在 InputStream 中获得针对您请求的 URL 的响应。以您的方式使用该流来呈现您想要的数据。例如,如果预期的数据是字符串,那么您可以简单地将这个流转换为字符串,方法如下:

    /**
     * Converts InputStream to String and closes the stream afterwards
     * @param is Stream which needs to be converted to string
     * @return String value out form stream or NULL if stream is null or invalid.
     * Finally the stream is closed too. 
     */
    public static String streamToString(InputStream is) {
        try {
            StringBuilder sb = new StringBuilder();
            BufferedReader tmp = new BufferedReader(new InputStreamReader(is),65728);
            String line = null;
    
            while ((line = tmp.readLine()) != null) {
                sb.append(line);
            }
    
            //close stream
            is.close();
    
            return sb.toString();
        }
        catch (IOException e) { e.printStackTrace(); }
        catch (Exception e) { e.printStackTrace(); }
    
        return null;
    }
    

    【讨论】:

    • 你的回答和raju的回答有什么区别?对于这一行:(new InputStreamReader(is,"iso-8859-1"),8)(new InputStreamReader(is),65728)
    • 他只是将字符编码设置为ASCII。但是,在我的回答中,我依赖于字符串的默认编码,即 UTF-8。默认情况下,所有 Json 和 XML 流都以 UTF-8 编码。因此,我想首先尝试使用默认编码是合理的,如果它不能按预期工作,那么请移至任何其他编码。或者与您的服务器核实以确认它使用哪种方案。 Besieds,关于缓冲区读取长度为 8 或 65728,我只是将其设置为更高的值,以便以更大的块而不是小的块读取数据。所以这取决于你。
    • 我明白了,我对 Raju 的回答有疑问。我如何实现你的代码?我输入后,如何使用它?
    • 将方法 streamToString 添加到新类中,例如 Helper,然后将其命名为:String result = Helper.streamToString(responseEntity.getContent());。之后,将 result 字符串分配给您的 TextView.setText() 或记录它
    • 好吧,我肯定在这里做错了什么。我对你和 raju 都有同样的错误。我在 WebView 上使用了String result,是否允许?对我来说,既然它是一个字符串值,为什么不呢?
    【解决方案3】:
    InputStream is = responseEntity.getContent();
     try{
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
             sb.append(reader.readLine() + "\n");
             String line="0";
             while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
              }
    
         String   result=sb.toString();
              is.close();
     }catch(Exception e){
                    Log.e("log_tag", "Error converting result "+e.toString());
              }
    

    你将拥有字符串“result”中的所有内容

    【讨论】:

    • 感谢您的回复,您介意解释一下“iso-8859-1”和“8”是什么意思吗?或者如果我不放下它会怎样?
    • 我收到一条错误消息,我需要使用 try-catch 异常。我应该怎么办?我确实在我的主要活动中使用了你的方法,这是放置此代码的错误位置吗?
    • 我的方法可能做错了什么。编译和构建项目后,我遇到了很多错误。您要我更新我的问题并发布所有错误消息吗?或者我可以压缩文件并发送给您检查吗?不用担心,我正在使用我愿意透露的测试 https 网站。
    猜你喜欢
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多