带有附加信息的MediaType

时间:2017-07-12 15:57:53

标签: android okhttp multipart okhttp3

创建Multipart /相关请求时。我们需要在type=MIME_TYPE字符串

中添加额外的Content-Type

Content-Type应如下所示

Content-Type: multipart/related; boundary=boundary_1324; type="application/json";

查看MultipartBody构造函数时

MultipartBody(ByteString boundary, MediaType type, List<Part> parts) {
    this.boundary = boundary;
    this.originalType = type;
    this.contentType = MediaType.parse(type + "; boundary=" + boundary.utf8());
    this.parts = Util.immutableList(parts);
  }

我们可以添加边界令牌。

但是这样打电话给MediaType.parse

final MediaType parse = MediaType.parse("multipart/related; type=application/json");

对象解析为null

任何人都知道如何添加type=application/json

1 个答案:

答案 0 :(得分:2)

引用该值,并且不包括尾随;

use $username instead of $loginuser;

这符合规范,该规范要求使用简单令牌或带引号的字符串。 https://tools.ietf.org/html/rfc2045#section-5.1

n.b。 MediaType不会为您提取类型,但不应该失败。

相关问题