有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java HttpURLConnection connect()、getInputStream()、getContent()、HttpGet

我使用HttpURLConnection类,但我对一些方法感到困惑

假设HttpURLConnection对象名为“c”

  1. 删除c.connect()将导致连接成功,并将毫无问题地检索连接结果

  2. c.getInputStream()和(InputStream)c.grtContent()的输出是相同的,那么它们之间的区别是什么

  3. 使用HttpGet将达到与HttpURLConnection相同的方法,那么这两种方法之间的区别是什么

  4. 在URLConnection上,HttpURLConnection的额外好处是什么

    c=(HttpURLConnection)(URL)。openConnection(); c、 connect()//添加或删除会产生相同的结果,那么此方法的用途是什么 InputStream=c.getInputStream(); InputStream2=(InputStream)c.getContent(); //stream和stream2是相同的,那么getInputStream()和getContent()之间的区别是什么呢

    //============================

    HttpGet c=新的HttpGet(url); HttpResponse response=c.execute(httpGet) InputStream3=响应。getEntity()。getContent(); //此外,stream3与stream&;流程2;那么,HttpGet和&;HttpURLConnection


共 (1) 个答案

  1. # 1 楼答案

    来自Android文档:

    [HttpURLConnection is] A URLConnection with support for HTTP-specific features.

    例如,您可以从HttpURLConnection检索HTTP方法或HTTP状态代码,它们是特定于HTTP的

    URLConnection类是:

    The abstract class URLConnection is the superclass of all classes that represent a communications link between the application and a URL

    通常的用法是:

    1. 创建一个URL对象
    2. 通过调用url.openConnection()获取URLConnection。返回的对象可以强制转换为HttpURLConnection
    3. 通过调用connection.getInputStream()方法获取InputStream
    4. 关闭连接(disconnect()方法)(请参阅@EJP注释)

    关于connect()方法,来自Oracle文档:

    You are not always required to explicitly call the connect method to initiate the connection. Operations that depend on being connected, like getInputStream, getOutputStream, etc, will implicitly perform the connection, if necessary.

    HttpGetHttpURLConnection之间的区别在于它们属于两个不同的库,但在功能上它们或多或少是相同的(现在HttpGet已被弃用和删除,因此在标准Android API中找不到它)