private String getHtmlContent(final String url) {
String result = "";// 返回的结果
StringBuffer resultBuffer = new StringBuffer();
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
GetMethod getMethod = new GetMethod(url);
// 使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
// getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GB2312");
getMethod.getParams().setContentCharset("GB2312");
try { // 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "+ getMethod.getStatusLine()); }
// 流式读取
// 读取内容
// byte[] responseBody = getMethod.getResponseBody();
// 处理内容
// String result = new String(responseBody,"GBK");
// result = getMethod.getResponseBodyAsString();
// System.out.println(result);
// System.out.println(getMethod.getResponseCharSet());
// 推荐做法
BufferedReader in = new BufferedReader(new InputStreamReader(
getMethod.getResponseBodyAsStream(), getMethod.getResponseCharSet()));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
result = new String(resultBuffer);
return result;
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
return result;
}
}
http://nwhy.org/android-httpclient-gethtmlcontent.html
分享到:
相关推荐
HttpClient抓取网页Demo,HttpClient 的入门示例,配合blog文章的附件
Android使用HttpClient发送请求、接收响应很简单,只要如下几步即可: Step1:创建HttpClient对象; Step2:如果需要发送GET请求,则创建HttpGet对象; 如果需要发送POST请求,则创建HttpPost对象; Step3:如果...
java httpclient 抓取 数据 和jar 包
Android 使用HttpClient代理
Java抓取https网页数据,解决peer not authenticated异常。导入eclipse就能运行,带有所用的jar包(commons-httpclient-3.1.jar,commons-logging.jar,httpclient-4.2.5.jar,httpcore-4.2.4.jar)
httpClient+jsoup抓取网页数据实例和jar包
Android使用HttpClient上传文件到服务器完整实例,包括要用到的jar都下libs下面,服务器的源码也有
Android HttpClient基本使用方法 测试过能用。
本例子是一个使用HttpClient和URLConnection获取网页html内容的小例子,获取到的源码不解析直接显示,技术比较简单,需要的朋友可以下载研究一下,项目编码GBK默认编译版本2.3.3
是android的jar中httpclient的源码,是org.apache.http包的源码。其中包含org.apache.http.HttpResponse类。 不是官网上下载的,官网上的源码里面没有org.apache.http.HttpResponse这个类。
Android Asynchronous HTTPClient的实现和优化
通过一个URLConnection或者一个保存有网页内容的字符串来初始化Parser,或者使用静态函数来生成一个Parser对象。 ParserFeedback的代码很简单,是针对调试和跟踪分析过程的,一般不需要改变。而使用Lexer???? ...
Httpclient上传文件,非常有用的jar包 apache-mime4j-0.6.jar httpmime-4.0.jar
Android利用HttpClient实现访问web服务器源码 剥离出HttpClient代码,用来做网页爬虫亦可
HttpClient网页抓取工具包整合,
Android的HttpClient开发实例。欢迎下载学习
Android-异步HttpClient框架,采用两种提交方式 Android-异步HttpClient框架(get和post) Android-异步HttpClient框架 csdn博客地址:http://blog.csdn.net/u014657752
Android平台HttpClient的使用-手机号码归属地查询
android2.1下 用httpclient拿取sgs.sgamer.com的新闻 用AppWidgetProvider展示 定时刷新 Notification 点击浏览