当我传递"json"时,getMimeTypeFromExtension返回null.作为扩展 [英] getMimeTypeFromExtension returns null when I pass "json" as extension

查看:369
本文介绍了当我传递"json"时,getMimeTypeFromExtension返回null.作为扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码返回null

The following code returns null

MimeTypeMap.getSingleton().getMimeTypeFromExtension("json");

我已经尝试过使用其他格式,例如mp4,png等...,它可以正常工作,但不能用于json.

I've tried it with other formats like mp4, png, etc... and it works fine but fails to work for json.

我也尝试过URLConnection.guessContentTypeFromName并且也得到了null.

I've also tried URLConnection.guessContentTypeFromName and got null as well.

那么我怎样才能得到一个我希望它是"application/json"的json的mime类型呢?

So how can I get the mime type of a json which I'm expecting it to be "application/json"?

临时解决方案

public static final String MIME_TYPE_JSON = "application/json";

private static final String EXTENSION_JSON = "json";

@Nullable
public static String getMimeType(final String path) {
    // StringUtil is my own util but you can use Guava for the same result
    String extension = StringUtil.getExtension(path).toLowerCase();
    return extension.equals(EXTENSION_JSON) ?
                MIME_TYPE_JSON : MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}

推荐答案

Android不支持"json"(JavaScript扩展为"js"),请检查源代码

Android does not support "json" (and "js" for javascript extension), check the source code here. and see the method that you are calling, it will return null if the given extension is not listed in the defined set of content type map.

这篇关于当我传递"json"时,getMimeTypeFromExtension返回null.作为扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆