HTTP访问 并得到返回结果 JSon

来源:互联网 发布:嫁给大山的女人知乎 编辑:程序博客网 时间:2024/05/16 05:08



/**
 * @author hongqian_li
 * 
 */


public class Https
{
    
    /**
     * @Description Https link and get Response conversion JSonObject
     * @param url   Address of the interface
     * @param map   Parameter collection   The map can be null
     * @return
     */
    public static JSONObject HttpsLinkAndRetJsonObject(String url, Map map)
    {
        JSONObject object = null;
        StringBuffer strbf = new StringBuffer();
        strbf.append(url);
        if (null != map)
        {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                strbf.append(entry.getKey() + "=" + entry.getValue() + "&");
            }
            strbf = new StringBuffer(strbf.substring(0, strbf.length() - 1));
        }
        
        try
        {
            URL connUrl = new URL(strbf.toString());
            URLConnection urlConnection = connUrl.openConnection();
            if (null == urlConnection)
                return null;
            InputStream inputStream = urlConnection.getInputStream();
            byte[] inputdata = new byte[inputStream.available()];
            inputStream.read(inputdata);
            String retcode = new String(inputdata, "utf-8");
            object = JSONObject.fromObject(retcode);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return object;
    }
    
    /**
     * @Description Https link and get Response conversion JSONArray
     * @param url   Address of the interface
     * @param map   Parameter collection   The map can be null
     * @return
     */
    public static JSONArray HttpsLinkAndRetJSONArray(String url, Map map)
    {
        JSONArray object = null;
        StringBuffer strbf = new StringBuffer();
        strbf.append(url);
        if (null != map)
        {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                strbf.append(entry.getKey() + "=" + entry.getValue() + "&");
            }
            strbf = new StringBuffer(strbf.substring(0, strbf.length() - 1));
        }
        
        try
        {
            URL connUrl = new URL(strbf.toString());
            URLConnection urlConnection = connUrl.openConnection();
            if (null == urlConnection)
                return null;
            InputStream inputStream = urlConnection.getInputStream();
            byte[] inputdata = new byte[inputStream.available()];
            inputStream.read(inputdata);
            String retcode = new String(inputdata, "utf-8");
            object = JSONArray.fromObject(retcode);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return object;
    }
    

}

所需要的jar包 下载地址:http://download.csdn.net/detail/a956959099/7478193

0 0
原创粉丝点击