`

Android网络连接2——HttpURLConnection

 
阅读更多

    HttpURLConnection类是URL代表的网络资源与应用程序的连接。应用程序通过HttpURLConnection对象向服务器发送,读取URL指定的网络资源。

    使用步骤:

   1、创建URL对象

   2、通过url.openConnection()方法创建URLConnection对象

   3、设置URLConnection的参数和普通请求属性

  

		//1、创建URL对象
			URL url = new URL("http://192.168.70.113:8008/upload/uploadAction.do");
			//2、获得HttpUrlConnection对象
			HttpURLConnection httpURLConnection = (HttpURLConnection) url
					.openConnection();
			//3、设置请求方法类型,注意:必须大写
			httpURLConnection.setRequestMethod("POST");
			//4、设置参数属性
			//向服务器发送请求数据
			httpURLConnection.setDoOutput(true);
			//获取服务器的返回数据
			httpURLConnection.setDoInput(true);
			//禁止httpURLConnection使用缓存
			httpURLConnection.setUseCaches(false);
			//
			httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
			httpURLConnection.setRequestProperty("Charset", "UTF-8");
			//设置实体消息的类型,"application/x-www-form-urlencoded" 表示实体类型的自定义表单
//			httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			//设置类型为上传图片
			httpURLConnection.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			//5、获取到服务端的输入输出流
		        InputStream is = httpURLConnection.getInputStream();
		        OutputStream out = httpURLConnection.getOutputStream();
                        //6、关闭流
                        is.close();out.close();

 

附件为上传下载案例:

 

 

分享到:
评论

相关推荐

    Android与Internet接轨案例源代码

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(PDF高清完整版3)(4-3)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全的目录

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(完整版附部分源码).pdf

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载...

    Google+Android+SDK开发范例大全

    ——HttpURLConnection与Service侦测服务 第9章 Google服务与Android混搭 9.1 Google帐号验证Token——AuthSub 9.2 Google搜索——AutoCompleteTextView与GoogleSearchAPI 9.3 前端产生QRCode二维条形码——...

    Google Android SDK 开发范例大全01

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK 开发范例大全02

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android sdk 开发范例大全 部分章节代码

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    Google Android SDK开发范例大全(完整版)

    8.6 即时访问网络图文件展示——HttpURLConnection 8.7 手机气象局,实时卫星云图——HttpURLConnection与URLConnection和运行线程 8.8 通过网络播放MP3——Runnable存储FileOutputStream技巧 8.9 设置远程下载音乐为...

    通过HttpUrlConnection开发客户端(Android)和服务端(ssh)交互的新闻网站

    服务端用ssh框架搭建 数据库...客户端用Android编译,HttpUrlConnection连接 通过json传输数据 部分电脑需要关闭防火墙 android真机和电脑连在同一网段,cmd——》ipconfig查找网段ip并在源码中修改 内附课程设计报告

Global site tag (gtag.js) - Google Analytics