Android 发送http请求实例

浏览:
字体:
发布时间:2013-12-20 16:18:41
来源:
           		URL url;			try {				OutputStream os = null;								url = new URL(uri);				HttpURLConnection conn = (HttpURLConnection) url.openConnection();				conn.setDoOutput(true);				conn.setDoInput(true);				conn.setRequestMethod("POST");				conn.setFixedLengthStreamingMode(packetData.length);				conn.setUseCaches(false);												os = conn.getOutputStream();				int offset = 0;				int len = READ_WRITE_SIZE;				while (offset < packetData.length){					if (offset + len > packetData.length){						len = packetData.length - offset;					}					os.write(packetData, offset, len);					os.flush();					offset += len;				}				InputStream fin = null;				fin = conn.getInputStream();				BufferedReader r = new BufferedReader(new InputStreamReader(fin)); 				StringBuilder total = new StringBuilder(); 				String line; 				while ((line = r.readLine()) != null) { 				    total.append(line); 				}				tv.setText("---> "+total);			} catch (MalformedURLException e) {				e.printStackTrace();			} catch (IOException e) {				e.printStackTrace();			}

需要注意的是不能在主线中进行http请求,需要

new Thread(mRun).start();  Runnable mRun = new Runnable(){}


>更多相关文章
24小时热门资讯
24小时回复排行
资讯 | QQ | 安全 | 编程 | 数据库 | 系统 | 网络 | 考试 | 站长 | 关于东联 | 安全雇佣 | 搞笑视频大全 | 微信学院 | 视频课程 |
关于我们 | 联系我们 | 广告服务 | 免责申明 | 作品发布 | 网站地图 | 官方微博 | 技术培训
Copyright © 2007 - 2024 Vm888.Com. All Rights Reserved
粤公网安备 44060402001498号 粤ICP备19097316号 请遵循相关法律法规
');})();