首页 手机话费充值 手机话费充值示例代码 获取商品列表[Java]

获取商品列表示例代码[Java]

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

获取商品列表

package api.jisuapi.mobilerecharge;
/**
 * 获取商品列表
 */
import api.util.HttpUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Goods {

	public static final String APPKEY = "your_appkey_here";// 你的appkey
	public static final String URL = "https://api.jisuapi.com/mobilerecharge/goods";

	public static void Get() {
		String result = null;
		String url = URL + "?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 = (JSONObject) json.opt("result");
				JSONArray list = resultarr.optJSONArray("list");
				for (int i = 0; i < list.size(); i++) {
					JSONObject obj = (JSONObject) list.opt(i);
					String name = obj.getString("name");
					String areaid = obj.getString("areaid");
					String company = obj.getString("company");
					String price = obj.getString("price");
					String amount = obj.getString("amount");
					System.out.println(name + " " + areaid + " " + company + " " + price + " " + amount);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}