以下代码返回null
$('.many-checkbox').on('change', function() {
doSomething();
});
我已经尝试过其他格式,比如mp4,png等......它工作正常,但无法为json工作。
我也试过了MimeTypeMap.getSingleton().getMimeTypeFromExtension("json");
并且也获得了空。
那么如何才能得到json的mime类型,我希望它是URLConnection.guessContentTypeFromName
?
临时解决方案
"application/json"
答案 0 :(得分:1)
答案 1 :(得分: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);
});
在home目录中,我有一个包含json文件(jo.json)的公用文件夹,服务器在请求时发送此文件。 jo.json
{
name: "inder"
, age: 24
}
并记录cat:
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这是唯一有效的方法。谢谢
答案 2 :(得分:0)
这是Android中的错误,已在Android Q Beta中修复。