Okhttp的MediaType

目录

1、MIME 类型

2、MediaType

2.1、MediaType的构造函数

2.2、type

2.3、subtype

2.4、charset

2.5、举例


1、MIME 类型

MIME (Multipurpose Internet Mail Extensions) 是描述消息内容类型的因特网标准。

MIME 消息能包含文本、图像、音频、视频以及其他应用程序专用的数据。

详细内容可见:MIME 参考手册

2、MediaType

MediaType指的是要传递的数据的MIME类型。

2.1、MediaType的构造函数

private MediaType(String mediaType, String type, String subtype, String charset) {
    this.mediaType = mediaType;
    this.type = type;
    this.subtype = subtype;
    this.charset = charset;
  }

MediaType对象包含了三种信息:type 、subtype以及charset,一般将这些信息传入parse()方法中,这样就可以解析出MediaType对象,比如 "text/x-markdown; charset=utf-8" ,type值是text,表示是文本这一大类;/后面的x-markdown是subtype,表示是文本这一大类下的markdown这一小类; charset=utf-8 则表示采用UTF-8编码。

2.2、type

/**
   * Returns the high-level media type, such as "text", "image", "audio", "video", or
   * "application".
   * 返回一个高级媒体类型,例如 text、image、audio、video、application
   */
  public String type() {
    return type;
  }

2.3、subtype

/**
   * Returns a specific media subtype, such as "plain" or "png", "mpeg", "mp4" or "xml".
   * 返回一个具体的媒体子类型,例如plain、png、mpeg、mp4、xml
   */
  public String subtype() {
    return subtype;
  }

2.4、charset

/**
   * Returns the charset of this media type, or null if this media type doesn't specify a charset.
   * 返回媒体类型的字符集,如果这个媒体类I型那个没有指定字符集的话,就返回null
   */
  public Charset charset() {
    return charset != null ? Charset.forName(charset) : null;
  }

2.5、举例

  • son : application/json
  • xml : application/xml
  • png : image/png
  • jpg : image/jpeg
  • gif : imge/gif
发布了632 篇原创文章 · 获赞 758 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/songzi1228/article/details/104371625