首页 菜谱大全 菜谱大全示例代码 根据ID查询详情[Java]

根据ID查询详情示例代码[Java]

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

根据ID查询详情

package api.jisuapi.recipe;

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

public class Detail {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/recipe/detail";
	public static final int id = 5;

	public static void Get() {
		String result = null;
		String url = URL + "?id=" + id + "&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 id = resultarr.getString("id");
				String classid = resultarr.getString("classid");
				String name = resultarr.getString("name");
				String peoplenum = resultarr.getString("peoplenum");
				String preparetime = resultarr.getString("preparetime");
				String cookingtime = resultarr.getString("cookingtime");
				String content = resultarr.getString("content");
				String pic = resultarr.getString("pic");
				String tag = resultarr.getString("tag");
				System.out.println(id + " " + classid + " " + name + " " + peoplenum + " " + preparetime + " "
						+ cookingtime + " " + content + " " + pic + " " + tag);
				if (resultarr.opt("material") != null) {
					JSONArray material = resultarr.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 (resultarr.opt("process") != null) {
					JSONArray process = resultarr.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();
		}
	}
}