JSOUP 請求JSON

JSOUP請求JSONhtml

    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());

可是出現問題了,請求就報錯:java

    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)

沒有指定類型。找了以下解決方案:json

     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() 去執行。app

不過我最後仍是換作用 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("關閉流出現異常!!!");
            }
        }
    }
相關文章
相關標籤/搜索