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;
	}
}



你可能感兴趣的:(exception,json,android,String,服务器,user)