2017-06-21 55 views
0

下面的代碼返回nullgetMimeTypeFromExtension返回null

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

我和其他格式,如MP4,PNG等嘗試它,而且它工作正常,但未能爲json工作。

我也試過URLConnection.guessContentTypeFromName並且也是空的。

那麼我怎樣才能得到一個json的MIME類型,我期待它是"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); 
} 

回答

0

修改答案。

所以我測試了幾件事,顯然沒有其他工作,然後urlConnection.getContenttype。我添加了一個我創建的示例。 Cart.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 Web服務器(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這是唯一的方法工作。謝謝

+0

如果在問題中提到的方法中發生確切的字符串比較,那麼這更準確嗎? – droidev

+0

https://developer.android.com/reference/java/net/URLConnection.html#guessContentTypeFromStream(java.io.InputStream) 由於文檔統計一些http服務器可以返回錯誤類型。它可以是更好的方法來得到我的從字節鍵入。 –

+0

這很有道理 – droidev

1

Android不支持「json」(和「js」爲javascript擴展名),請檢查源代碼here。和see您調用的方法,如果給定的擴展未在定義的內容類型映射中列出,它將返回null。