android网络应用2——URLConnection
来源:互联网 发布:brew install php fpm 编辑:程序博客网 时间:2024/06/09 19:46
ps:以下内容来自对李刚的《疯狂Android讲义》的学习
URL 的openConnection()方法返回一个URLConnection对象,此对象代表程序与URL的通信连接,程序可以通过URLConnection对象向此URL发送请求,读取此URL代表的远程资源。
通常创建一个和URL对象的远程连接,发送请求并读取远程资源有下面4个步骤。
1通过URL 的openConnection()方法创建一个URLConnection对象;
2设置URLConnection的参数和请求属性
3如果是发送的是get请求,使用connect方法建立与远程资源的实际连接即可;如果发送的是post请求,则需要URLConnection实例对应的输出流来发送参数。
4远程资源变为可用,程序可以访问远程资源的头字段,或者通过输入流读取远程资源。
在建立与远程资源的实际连接之前,可以使用以下方法设置请求头字段:
setAllowUserInteraction:设置allowUserInteraction头字段的值,如果为 true,则在允许用户交互(例如弹出一个验证对话框)的上下文中对此 URL 进行检查。
setDoInput:设置doInput头字段的值,如果为true,则这个URLConnection对象允许输入
setDoOutput:设置doOutput头字段的值,如果为true,则这个URLConnection对象允许输出
setUseCaches:设置useCaches头字段的值,如果为true,则使用缓存cache
setIfModifiedSince;设置If-Modified-Since头字段的值,值类型为datatime,表明如果这个时间之后资源被修改了,服务器就会返回给客户端最新的
setRequestProperty(String key,String value):设置通用头字段,各字段的代表的意义请看http协议。
addRequestProperty(String key,String value):为key请求的头字段增加value值,该方法不会覆盖原头字段的值,而是将新值加在上面。
当资源可用时可以用以下方法访问头字段与内容。
- public StringgetHeaderField(String name)
得到指定头域的名称。 - public ObjectgetContent() throws IOException
得到URL连接的内容。该方法会先调用getContentType方法得到对象的内容类型,再做相应的处理。 - public InputStream getInputStream() throws IOException
- public OutputStream getOutputStream()throws IOException
得到URLConnection的输入或输出流。
- public URLgetURL()
得到URLConnection对象的URL域。 - public intgetContentLength()
得到content-type头域的值。 - public StringgetContentEncoding()
得到content-encoding头域的值。 - public longgetDate()
得到date头域的值。 - public longgetLastModified()
得到last-modified头域的值。 - public long getExpiration()
得到expiration头域的值。
public static String sendGet(String url)
{
BufferedReader in=null;
String result = "";
try
{
URL theURL=new URL(url);
URLConnection conn=theURL.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.connect();
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null)
{
result+=line+"\n";
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(in!=null)
{
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
}
使用URLConnection发送post请求
public static String sendPost(String url,String params)
{
BufferedReader in=null;
PrintWriter out=null;
String result = "";
try
{
URL theURL=new URL(url);
URLConnection conn=theURL.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
//发送post请求必须设置以下两行
conn.setDoInput(true);
conn.setDoOutput(true);
//获取URLConnection对应的输出流并发送参数
out=new PrintWriter(conn.getOutputStream());
out.print(params);
//flush输出流的缓存
out.flush();
in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while((line=in.readLine())!=null)
{
result+=line+"\n";
}
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
if(in!=null)
{
try
{
in.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null)
{
out.close();
}
}
return result;
}
可知,发送get请求只需要调用URLConnection方法的connect()方法建立实际的连接即可,而发送post请求则需要获取URLConnection的OutputStream.然后向网络中输出请求参数
深入理解http协议:http://www.blogjava.net/zjusuyong/articles/304788.html
http缓存介绍:http://blog.csdn.net/nkorange/article/details/7891645
- android网络应用2——URLConnection
- android网络应用2——URLConnection
- Android 网络连接——URLConnection
- Android 网络编程(3)——使用URLConnection提交请求
- android网络之URLconnection
- android网络应用URLConnection提交请求GET和POST
- Java学习总结12——网络2(URLConnection详解)
- 网络编程(2)——连接父类URLConnection
- 网络技术之——网络连接URLConnection、单/多线程下载
- Java网络编程——第七章 URLConnection
- Android中的网络编程系列(一):URLConnection
- Android网络编程之URLConnection&HttpURLConnection
- Android之使用URLConnection进行网络编程
- Android网络连接----使用URLConnection连接网络简单例子
- android获取URLConnection和HttpClient网络请求响应码
- android获取URLConnection和HttpClient网络请求响应码
- android获取URLConnection和HttpClient网络请求响应码
- android获取URLConnection和HttpClient网络请求响应码
- 杭电OJ题 1040 As Easy As A+B 解题报告
- Tabular: 在 Vim 中对齐文本
- 详解WINCE的控制面板
- linux input 设备的测试函数
- 指针赋值与指针传递
- android网络应用2——URLConnection
- 熟悉struts2MVC模式之用属性驱动传参例子
- Source Taste: Struts 2: com.opensymphony.xwork2.interceptor.ParametersInterceptor
- sed 命令
- Entity SQL 使用技巧
- Struts2的dojo使用与/template/ajax/head.ftl not found
- 学习方法
- 杭电OJ题 1106 排序 解题报告
- android4.1~Systrace