【问题标题】:getMimeTypeFromExtension returns null when I pass "json" as extension当我将“json”作为扩展名传递时,getMimeTypeFromExtension 返回 null
【发布时间】:2017-11-23 19:52:25
【问题描述】:

以下代码返回null

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

我已经尝试过使用其他格式,如 mp4、png 等...效果很好,但无法用于 json。

我也试过URLConnection.guessContentTypeFromName 也得到了null。

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

临时解决方案

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 mime-types


    【解决方案1】:

    Android 不支持“json”(以及用于 javascript 扩展的“js”),请查看源代码here。和 see 您正在调用的方法,如果给定的扩展名未列在定义的内容类型映射集中,它将返回 null。

    【讨论】:

      【解决方案2】:

      修改后的答案。

      所以我测试了一些东西,显然除了 urlConnection.getContenttype 之外没有任何效果。我正在添加一个我创建的示例。 购物车.java

      package com.plumtestongo.sample;
      import android.net.Uri;
      import android.os.AsyncTask;
      import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      import android.util.Log;
      import android.webkit.MimeTypeMap;
      
      import java.io.IOException;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLConnection;
      
      public class Cart extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              new background().execute();
          }
      
          private class background extends AsyncTask<Void,Void,String>{
      
              @Override
              protected String doInBackground(Void... params) {
                  String mime1;
                  String mime2;
                  String mime3;
                  String mime4;
                  boolean hasMime;
      
                  Uri uri = Uri.parse("http://192.168.0.6:3000/jo.json").buildUpon().build();
                  try {
                      URL url = new URL(uri.toString());
                      URLConnection urlConnection = url.openConnection();
                      urlConnection.connect();
                      mime1 = URLConnection.guessContentTypeFromName("jo.json");
                      mime2 = urlConnection.getContentType();
                      mime3 = URLConnection.guessContentTypeFromStream(urlConnection.getInputStream());
                      mime4 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url.toString()));
                      hasMime = MimeTypeMap.getSingleton().hasMimeType("application/json");
                      Log.i(Cart.this.getClass().getName(),"Have extension we are looking for:"+hasMime);
                      Log.i(Cart.this.getClass().getName(),"From Name: Correct mime type is:"+mime1);
                      Log.i(Cart.this.getClass().getName(),"From Content type: Correct mime type is:"+mime2);
                      Log.i(Cart.this.getClass().getName(),"From Input stream type: Correct mime type is:"+mime3);
                      Log.i(Cart.this.getClass().getName(),"From Extension type: Correct mime type is:"+mime4);
      
                  } catch (MalformedURLException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  return null;
              }
      
      
          }
      
      }
      

      我在本地机器上托管 node.js 网络服务器(express.js)。 app.js

      var express = require('express');
      var app = express();
      const port = 3000;
      app.get('/', function (req, res) {
          res.send('hello world')
      });
      var stat = express.static('./public');
      app.use(stat);
      app.listen(port, function () {
          console.log('magic happening at:' + port);
      });
      

      在主目录中,我有一个公用文件夹,其中包含一个 json 文件(jo.json),服务器会在请求时发送此文件。 jo.json

      {
          name: "inder"
          , age: 24
      }
      

      和日志猫:

      06-21 11:25:38.581 6441-6493/com.plumtestongo.sample I/com.plumtestongo.sample.Cart: Have extension we are looking for:false
      06-21 11:25:38.581 6441-6493/com.plumtestongo.sample I/com.plumtestongo.sample.Cart: From Name: Correct mime type is:null
      06-21 11:25:38.581 6441-6493/com.plumtestongo.sample I/com.plumtestongo.sample.Cart: From Content type: Correct mime type is:application/json
      06-21 11:25:38.581 6441-6493/com.plumtestongo.sample I/com.plumtestongo.sample.Cart: From Input stream type: Correct mime type is:null
      06-21 11:25:38.581 6441-6493/com.plumtestongo.sample I/com.plumtestongo.sample.Cart: From Extension type: Correct mime type is:null
      

      使用 urlConnection.getContenttype 这是唯一有效的方法。谢谢

      【讨论】:

      • 当问题中提到的方法中发生精确的字符串比较时,如何更准确?
      • developer.android.com/reference/java/net/… 作为文档统计,一些 http 服务器可能会返回错误的类型。从字节中获取我的类型可能是更好的方法。
      • 有道理
      • 仍然返回 null
      • 我已经修改了答案。
      【解决方案3】:

      这是 Android 中的一个错误,已在 Android Q Beta 中修复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-13
        • 1970-01-01
        • 2015-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多