Jsoup请求http或https返回json字符串工具类详解编程语言

 Jsoup请求http或https返回json字符串工具类

所需要的jar包如下:

jsoup-1.8.1.jar

依赖jar包如下:

httpclient-4.5.4.jar; 
httpclient-cache-4.5.4.jar; 
httpclient-win-4.5.4.jar; 
httpcore-4.4.8.jar; 
httpcore-ab-4.4.8.jar; 
httpcore-nio-4.4.8.jar; 
httpmime-4.5.4.jar;

Jsoup返回json字符串工具类JsoupHelper.java:

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import java.util.Map.Entry; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.util.EntityUtils; 
import org.jsoup.Connection; 
import org.jsoup.Connection.Response; 
import org.jsoup.Jsoup; 
 
public class JsoupHelper { 
 
    /** 
     * 获取连接 
     *  
     * @param url 
     *            请求url 
     * @param params 
     *            参数 
     * @param charset 
     *            参数编码方式 
     * @param headers 
     *            头部信息 
     * @return 
     */ 
    public static Connection getConnection(String url, Map<String, String> params, String charset, 
            Map<String, String> headers) { 
        if (params != null) { 
            List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
            Iterator<Entry<String, String>> iter = params.entrySet().iterator(); 
            while (iter.hasNext()) { 
                Entry<String, String> entry = iter.next(); 
                String key = entry.getKey(); 
                String value = entry.getValue(); 
                paramList.add(new BasicNameValuePair(key, value)); 
            } 
            try { 
                String paramStr = EntityUtils.toString(new UrlEncodedFormEntity(paramList, charset)); 
                StringBuffer sb = new StringBuffer(); 
                sb.append(url); 
                if (url.indexOf("?") > 0) { 
                    sb.append("&"); 
                } else { 
                    sb.append("?"); 
                } 
                sb.append(paramStr); 
                url = sb.toString(); 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } 
        } 
        Connection conn = Jsoup.connect(url); 
        conn.timeout(10000); // 10秒超时 
        conn.ignoreContentType(true); 
 
        if (headers != null) { 
            Iterator<Entry<String, String>> iter = headers.entrySet().iterator(); 
            while (iter.hasNext()) { 
                Entry<String, String> entry = iter.next(); 
                String key = entry.getKey(); 
                String value = entry.getValue(); 
                conn.header(key, value); 
            } 
        } 
        return conn; 
    } 
 
    public static String getJson(String url, Map<String, String> params, String charset, 
            Map<String, String> headers) { 
        String result = ""; 
        try { 
            Connection conn = getConnection(url, params, charset, headers); 
            Response response = conn.execute(); 
            result = response.body(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return result; 
    } 
 
}

调用代码:

import java.util.HashMap; 
import java.util.Map;
public class JsoupTest { public static Map<String, String> headers = null; static { headers = new HashMap<String, String>(); headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36"); headers.put("Accept-Language", "zh-CN,zh;q=0.8"); headers.put("Accept-Encoding", "gzip, deflate, sdch, br"); headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); headers.put("Connection", "Keep-Alive"); headers.put("Content-Type", "application/json;charset=UTF-8"); } public void test() { String url = "http://xxxxxx"; String content = JsoupHelper.get(url, null, "utf-8", headers); System.out.println("返回的json字符串:" + content); } }

获取Json串后解析,获取key和value值,参考:http://www.cnblogs.com/lizm166/p/8078846.html

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16828.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论