首页 菜谱大全 菜谱大全示例代码 按分类检索[Java]

按分类检索示例代码[Java]

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

按分类检索

package api.jisuapi.recipe;

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

public class Byclass {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/recipe/byclass";
	public static final int classid = 2;// 分类id(二级id)
	public static final int start = 0;
	public static final int num = 10;

	public static void Get() {
		String result = null;
		String url = URL + "?classid=" + classid + "&start=" + start + "&num=" + num + "&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 num = resultarr.getString("num");
				System.out.println(num);
				if (resultarr.opt("list") != null) {
					JSONArray list = resultarr.optJSONArray("list");
					for (int j = 0; j < list.size(); j++) {
						JSONObject list_ = (JSONObject) list.opt(j);
						String id = list_.getString("id");
						String classid = list_.getString("classid");
						String name = list_.getString("name");
						String peoplenum = list_.getString("peoplenum");
						String preparetime = list_.getString("preparetime");
						String cookingtime = list_.getString("cookingtime");
						String content = list_.getString("content");
						String pic = list_.getString("pic");
						String tag = list_.getString("tag");
						System.out.println(id + " " + classid + " " + name + " " + peoplenum + " " + preparetime + " "
								+ cookingtime + " " + content + " " + pic + " " + tag);
						if (list_.opt("material") != null) {
							JSONArray material = list_.optJSONArray("material");
							for (int i = 0; i < material.size(); i++) {
								JSONObject obj = (JSONObject) material.opt(i);
								String mname = obj.getString("mname");
								String type = obj.getString("type");
								String amount = obj.getString("amount");
								System.out.println(mname + " " + type + " " + amount);
							}
						}
						if (list_.opt("process") != null) {
							JSONArray process = list_.optJSONArray("process");
							for (int i = 0; i < process.size(); i++) {
								JSONObject obj = (JSONObject) process.opt(i);
								String pcontent = obj.getString("pcontent");
								String pic_ = obj.getString("pic");
								System.out.println(pcontent + " " + pic_);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}