• JSOUP 请求JSON


    JSOUP请求JSON

        Document doc = Jsoup
                .connect(Constant.DATA_URL)
                .header("Accept", "*/*")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                .timeout(10000).get();
        Element body = doc.body();
        JSONObject json = JSONObject.fromObject(body.text());

    但是出现问题了,请求就报错:

        org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/json;charset=UTF-8, URL=http://www.baidu.com/
            at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:600)
            at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
            at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
            at org.jsoup.helper.HttpConnection.get(HttpConnection.java:216)

    没有指定类型。找了如下解决方案:

         Response res = Jsoup.connect(Constant.DATA_URL)
                .header("Accept", "*/*")
                .header("Accept-Encoding", "gzip, deflate")
                .header("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
                .header("Content-Type", "application/json;charset=UTF-8")
                .header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0")
                .timeout(10000).ignoreContentType(true).execute();//.get();
        String body = res.body();
        JSONObject json = JSONObject.fromObject(body);

    上面其实关键点在于:ignoreContentType(true) ,这个是忽略请求类型。建议用execute() 去执行,如果用get 去执行的话,返回来是一个 HTML  页面包裹的 JSON  ,你处理起来稍微有点费劲。而且你要处理换行,你看到有换行了吧, JSON  正确的格式是一行返回,多行的话转成 JSON  对象的时候就会报错。所以还是推荐用execute() 去执行。

    不过我最后还是换做用 HttpConnection  来解决。

        InputStreamReader reader = null;
        BufferedReader in = null;
        try {
            URL url = new URL(Constant.DATA_URL);
            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(1000);
            reader = new InputStreamReader(connection.getInputStream(), "UTF-8");
            in = new BufferedReader(reader);
            String line = null; // 每行内容
            StringBuffer content = new StringBuffer();
            while ((line = in.readLine()) != null) {
                content.append(line);
            }
            if (StringUtils.isNotBlank(content)) {
                String jsonStr = content.toString().replaceAll("\n", "");
                data = JSONObject.fromObject(jsonStr);
            }
        } catch (SocketTimeoutException e) {
            System.out.println("连接超时!!!");
        } catch (JSONException e) {
            System.out.println("网站响应不是json格式,无法转化成JSONObject!!!");
        } catch (Exception e) {
            System.out.println("连接网址不对或读取流出现异常!!!");
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    System.out.println("关闭流出现异常!!!");
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    System.out.println("关闭流出现异常!!!");
                }
            }
        }
  • 相关阅读:
    paper 66: MATLAB函数—disp的使用
    paper 65 :尺度不变特征变换匹配算法[转载]
    paper 64:尺度空间(Scale space)理论
    paper 63 :函数比较:imfilter与fspecial
    paper 62:高斯混合模型(GMM)参数优化及实现
    paper 61:计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
    paper 60 :转载关于视觉SCI期刊
    paper 59:招聘
    paper 58 :机器视觉学习笔记(1)——OpenCV配置
    paper 57 :颜色直方图的代码
  • 原文地址:https://www.cnblogs.com/interdrp/p/9275296.html
Copyright © 2020-2023  润新知