码迷,mamicode.com
首页 > 移动开发 > 详细

Android面向HTTP协议发送get请求

时间:2014-08-17 22:37:32      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   java   io   数据   ar   2014   

/**
	 * 採用get请求的方式
	 * 
	 * @param username
	 * @param password
	 * @return null表示求得的路径有问题,text返回请求得到的数据
	 */
	public static String getRequest(String username, String password) {
		try {
			String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet?username="
					+ URLEncoder.encode(username)
					+ "&password="
					+ URLEncoder.encode(password);
			URL url = new URL(path);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setReadTimeout(500);
			conn.setRequestMethod("GET");
			int code = conn.getResponseCode();
			if (code == 200) {
				// 请求成功
				InputStream is = conn.getInputStream();
				String text = StreamUtil.readStream(is);
				return text;
			} else {
				// 请求失败
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

Android面向HTTP协议发送get请求,布布扣,bubuko.com

Android面向HTTP协议发送get请求

标签:android   blog   http   java   io   数据   ar   2014   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3918283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!