JSOUP 教程,JSOUP請求JSON ,JSOUP返回JSON 數據

原文連接:http://www.sojson.com/blog/174.htmlhtml

在用習慣了 JSOUP  後,由於那種鏈式結構,很是喜歡,故想用它來請求接口,構造請求頭的時候很是方便。其實它必須是支持的,由於底層使用的仍是 HttpConnection  作爲處理的,代碼以下:java

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

 

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

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  ,你處理起來稍微有點費勁。返回狀況以下:app

並且你要處理換行,你看到有換行了吧, JSON  正確的格式是一行返回,多行的話轉成 JSON  對象的時候就會報錯。因此仍是推薦用execute() 去執行。網站

不過我最後仍是換作用 HttpConnection  來解決。url

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("關閉流出現異常!!!");
    		}
    	}
    }

建議API JSON 接口請求,仍是用正常的 HTTP  請求吧。code

相關文章
相關標籤/搜索