首页 笑话大全 笑话大全示例代码 查询图片笑话[Java]

查询图片笑话示例代码[Java]

作者:xiezhongpian 阅读数:1570 上传时间:2017-05-09

查询图片笑话

package api.jisuapi.xiaohua;

import api.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Pic {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/xiaohua/pic";
	public static final int pagenum = 1;
	public static final int pagesize = 1;
	public static final String sort = "addtime";// addtime/rand

	public static void Get() {
		String result = null;
		String url = URL + "?pagenum=" + pagenum + "&pagesize=" + pagesize + "&sort=" + sort + "&appkey=" + APPKEY;

		try {
			result = HttpUtil.sendGet(url, "utf-8");
			JSONObject json = JSONObject.fromObject(result);
			if (json.getInt("status") != 0) {
				System.out.println(json.getString("msg"));
			} else {
				JSONObject resultarr = json.optJSONObject("result");
				String total = resultarr.getString("total");
				String pagenum = resultarr.getString("pagenum");
				String pagesize = resultarr.getString("pagesize");
				System.out.println(total + " " + pagenum + " " + pagesize);
				if (resultarr.opt("list") != null) {
					JSONArray list = resultarr.optJSONArray("list");
					for (int i = 0; i < list.size(); i++) {
						JSONObject obj = (JSONObject) list.opt(i);
						String content = obj.getString("content");
						String pic = obj.getString("pic");
						String addtime = obj.getString("addtime");
						String url1 = obj.getString("url");
						System.out.println(content + " " + pic + " " + addtime + " " + url1);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}