Android客户端处理服务器端返回的Json格式数据

客户端采用Get方式读取服务器端的数据,并处理此Json格式的数据

public class ConnectWeb {
	public static String path = "http://192.168.1.9:8080/AndroidWeb/";

	// 访问网站数据库获取数据
	private String connWeb(String url) {
		String str = "";
		try {
			HttpGet request = new HttpGet(url); 
			HttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);
			if (response.getStatusLine().getStatusCode() == 200) {
				str = EntityUtils.toString(response.getEntity());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
	//查询功能// 获取推荐商品
	public List<Goods> getPopList() {
		List<Goods> mylist = new ArrayList<Goods>();
		String url = path + "goodsAction.action?type=pop";
		String str = connWeb(url);

		try {
			JSONObject job = new JSONObject(str);
			JSONArray jay = job.getJSONArray("glist");
			for (int i = 0; i < jay.length(); i += 1) {
				JSONObject temp = (JSONObject) jay.get(i);
				Goods goods = new Goods();
				goods.setId(temp.getInt("id"));
				goods.setBrand(temp.getString("brand"));
				goods.setPrice((float) temp.getDouble("price"));
				goods.setDiscount((float) temp.getDouble("discount"));
				goods.setBcount(temp.getInt("bcount"));
				goods.setDes(temp.getString("des"));
				goods.setPic(temp.getString("pic"));
				goods.setDir(path + temp.getString("dir"));
				goods.setGid(temp.getString("gid"));
				goods.setType(temp.getInt("type"));
				goods.setPop(temp.getInt("pop"));
				mylist.add(goods);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mylist;
	}
	//用户登录判断
	public User userLogin(String uid,String pwd) {
		User user=null;
		String url = path + "usersAction.action?uid="+uid+"&pwd="+pwd;
		String str = connWeb(url);
		try {
			JSONObject job = new JSONObject(str);
			JSONObject job2 =(JSONObject) job.get("user");

			user=new User();
			user.setId(job2.getInt("id"));
			user.setUid(job2.getString("uid"));
			user.setUserPwd(job2.getString("pwd"));
			System.out.println("aaa:"+job.getInt("id")+","+job.getString("uid")+","+job.getString("pwd"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	
	//增加功能//增加用户订单
	public boolean addBill(String uid,String gids,String gnums,String btime,String btype,String address) {
		boolean pan=true;
		System.out.println("gid:"+gids+",gnums:"+gnums+",btype:"+btype+",address:"+address);
		String url = path + "billAction.action?type=add&uid="+uid+"&gids="+gids+"&gnums="+gnums+"&btime="+btime+"&btype="+btype+"&address="+address;
		String str = connWeb(url);

		try {
			JSONObject job = new JSONObject(str);
			pan=job.getBoolean("msg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pan;
	}
}



  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值