Android HttpURLConnection 基础使用
2013-05-20 15:35
471 查看
最基本的使用代码:
(访问baidu首页)
记得加上Internet权限:
默认的,不给urlConnection添加任何属性的话是使用Get方法,如果用Post可以:
当然还可以用setRequestProperty方法给请求添加:Host,Content-Type,Content-Lenth,Authentication等参数
再使用Post的时候还有一个注意点在官方文档中提及的:上传数据到服务器时为了达到最好的性能,你可以在知道数据固定大小长度的情况下使用 setFixedLengthStreamingMode(int) 方法,或者在不知道长度的情况下使用setChunkedStreamingMode(int)。因为如果不这样的话,HttpURLConnection 在发生请求之前是将数据全部放到内存里面的,这样浪费内存(会造成OutOfMemoryError)而且延时。这个东西再这里详细说了:http://www.mzone.cc/article/198.html
附带一个Post xml的例子:
这个例子的第7行是对空格的处理,理由是Android4.0+和2.0+的问题:/article/8333245.html
(访问baidu首页)
HttpURLConnection urlConnection = null; try { URL url = new URL("http://www.baidu.com/"); urlConnection = (HttpURLConnection)url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); String result = readInStream(in); handleResult(result); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { urlConnection.disconnect(); }
private String readInStream(InputStream in) { Scanner scanner = new Scanner(in).useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : ""; }
记得加上Internet权限:
<uses-permission android:name="android.permission.INTERNET"/>不然就会有UnKnownHostException.
默认的,不给urlConnection添加任何属性的话是使用Get方法,如果用Post可以:
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
当然还可以用setRequestProperty方法给请求添加:Host,Content-Type,Content-Lenth,Authentication等参数
再使用Post的时候还有一个注意点在官方文档中提及的:上传数据到服务器时为了达到最好的性能,你可以在知道数据固定大小长度的情况下使用 setFixedLengthStreamingMode(int) 方法,或者在不知道长度的情况下使用setChunkedStreamingMode(int)。因为如果不这样的话,HttpURLConnection 在发生请求之前是将数据全部放到内存里面的,这样浪费内存(会造成OutOfMemoryError)而且延时。这个东西再这里详细说了:http://www.mzone.cc/article/198.html
附带一个Post xml的例子:
HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setChunkedStreamingMode(0); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type", ("application/xml; charset=utf-8").replaceAll("\\s", "")); urlConnection.setRequestProperty("Content-Length", String.valueOf(Xml.getBytes().length)); OutputStream out = urlConnection.getOutputStream(); out.write(Xml.getBytes()); out.close(); int responseCode = urlConnection.getResponseCode(); InputStream in = null; if (responseCode == 200) { in = new BufferedInputStream(urlConnection.getInputStream()); } else { in = new BufferedInputStream(urlConnection.getErrorStream()); } String result = readInStream(in); handleResult(result); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { urlConnection.disconnect(); }
这个例子的第7行是对空格的处理,理由是Android4.0+和2.0+的问题:/article/8333245.html
相关文章推荐
- HTTP基础与Android之——使用HttpClient和HttpURLConnection
- Android开发-基础网络组件(1)使用HttpURLConnection登陆-注意打开网络需要在线程中执行-主线程不支持
- HTTP基础与Android之(安卓与服务器通信)——使用HttpClient和HttpURLConnection
- Android App开发基础篇—HttpURLConnection基础使用
- 【Android基础知识】HttpUrlConnection使用doGet和doPost方式和服务器交互
- [Android基础]Android中使用HttpURLConnection
- HTTP基础与Android之——使用HttpClient和HttpURLConnection
- Android开发-基础网络组件(1)使用HttpURLConnection登陆-AndroidStudio
- android 6.0使用httpUrlConnection代替httpClient
- Android之使用HttpURLConnection连接网络
- Android中,HttpURLConnection与Apache的DefaultHttpClient哪个更被推荐使用?
- Android学习之使用HttpURLConnection上传单个文件
- Android上使用HttpURLConnection发送HTTP请求
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- Android学习之使用HttpURLConnection同时上传文本和多个文件
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android 使用HttpURLConnection 实现多线程下载
- Android中版本更新(使用HttpURLConnection网络请求)
- 安卓基础:HttpURLConnection的使用
- Android的网络应用-使用HttpURLConnection