Android HttpURLConnection和HttpClient获取网络内容
2013-06-15 15:01
585 查看
Android开发网络相关应用,可以用HttpURLConnection和HttpClient这两个类来
获取网络相关内容。
1、用HttpURLConnection获取网络内容
核心代码如下,网络内容用string content返回:
public static String getContent(String url) throws Exception{
// URL
URL Url = new URL(url);
// HttpURLConnection
HttpURLConnection httpconn = (HttpURLConnection) Url.openConnection();
//连接 超时时间
httpconn.setConnectTimeout(3000);
//Socket 超时时间
httpconn.setReadTimeout(5000);
String content = "";
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
// InputStreamReader
InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),
"utf-8");
//int len = httpconn.getContentLength();
//System.out.printf("len[%d]\n",len);
//read type 1
int i;
while ((i = isr.read()) != -1) {
content = content (char) i;
}
isr.close();
}
//disconnect
httpconn.disconnect();
return content;
}
2、用HttpClient获取网络内容
核心代码如下,网络内容用string content返回:
public static String getContent(String url) throws Exception{
StringBuilder
sb = new StringBuilder();
DefaultHttpClient
client = new DefaultHttpClient();
HttpParams
httpParams = client.getParams();
//连接 超时时间
HttpConnectionParams.setConnectionTimeout(httpParams,
3000);
//Socket 超时时间
HttpConnectionParams.setSoTimeout(httpParams,
5000);
HttpResponse
response = client.execute(new HttpGet(url));
HttpEntity
entity = response.getEntity();
if
(entity != null) {
BufferedReader
reader =
new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
String
line = null;
while
((line = reader.readLine())!= null){
sb.append(line
"\n");
}
reader.close();
}
return
sb.toString();
}
获取网络相关内容。
1、用HttpURLConnection获取网络内容
核心代码如下,网络内容用string content返回:
public static String getContent(String url) throws Exception{
// URL
URL Url = new URL(url);
// HttpURLConnection
HttpURLConnection httpconn = (HttpURLConnection) Url.openConnection();
//连接 超时时间
httpconn.setConnectTimeout(3000);
//Socket 超时时间
httpconn.setReadTimeout(5000);
String content = "";
if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
// InputStreamReader
InputStreamReader isr = new InputStreamReader(httpconn.getInputStream(),
"utf-8");
//int len = httpconn.getContentLength();
//System.out.printf("len[%d]\n",len);
//read type 1
int i;
while ((i = isr.read()) != -1) {
content = content (char) i;
}
isr.close();
}
//disconnect
httpconn.disconnect();
return content;
}
2、用HttpClient获取网络内容
核心代码如下,网络内容用string content返回:
public static String getContent(String url) throws Exception{
StringBuilder
sb = new StringBuilder();
DefaultHttpClient
client = new DefaultHttpClient();
HttpParams
httpParams = client.getParams();
//连接 超时时间
HttpConnectionParams.setConnectionTimeout(httpParams,
3000);
//Socket 超时时间
HttpConnectionParams.setSoTimeout(httpParams,
5000);
HttpResponse
response = client.execute(new HttpGet(url));
HttpEntity
entity = response.getEntity();
if
(entity != null) {
BufferedReader
reader =
new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"), 8192);
String
line = null;
while
((line = reader.readLine())!= null){
sb.append(line
"\n");
}
reader.close();
}
return
sb.toString();
}
相关文章推荐
- android开发-通过HttpURLConnection获取网络内容,Bitmap把二进制格式的图片转为位图
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- Android 网络请求HttpURLConnection 和 HttpClient详解
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例
- android 网络通信之HttpURLConnection与HttpClient
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android HttpURLConnection获取网络下载图片POST请求方式
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android访问网络,HttpURLConnection还是HttpClient?
- Android网络连接之HttpURLConnection和HttpClient
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- Android之B面试题④访问网络,使用HttpURLConnection还是HttpClient?
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- Android网络访问之HttpURLConnection和HttpClient
- Android两种网络访问方式(HttpClient HttpUrlConnection)的比较及三种传参方式介绍
- Android访问网络,使用HttpURLConnection还是HttpClient?
- Android--网络连接之HttpURLConnection和HttpClient