Android:简单联网获取网页代码

设置权限,在AndroidManifest.xml加入
<uses-permission android:name="android.permission.INTERNET"/>
public class MainActivity extends Activity {
private EditText address;
private Button getbutton;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
//版本4.0后需加这个,不然就报错android.os.NetworkOnMainThreadException
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
//
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
//初始化
address = (EditText) findViewById(R.id.address);
getbutton = (Button) findViewById(R.id.getbutton);
text = (TextView) findViewById(R.id.text);
getbutton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url = address.getText().toString();
getPDAServerData(url);
}
});
}
public void getPDAServerData(String url) {
HttpClient client = new DefaultHttpClient();
HttpPost request;
try {
request = new HttpPost(url);
//调用HttpClient对象的execute(HttpUriRequest request)发送请求,返回一个HttpResponse
HttpResponse response = client.execute(request);
//返回响应码为200
if (response.getStatusLine().getStatusCode() == 200) {
//从响应中获取消息实体
HttpEntity entity = response.getEntity();
if (entity != null) {
String out = EntityUtils.toString(entity);
text.setText(out);
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Android:简单联网获取网页代码的更多相关文章
- Windows下比较简单的获取网页源码的方法
第一个方法是使用MFC里面的 <afxinet.h> CString GetHttpFileData(CString strUrl) { CInternetSession Session( ...
- c#获取网页代码、数据、资源
//WebClient取网页源码 private string GetHtmlSource(string Url) { try { System.Net.WebClient wc = new Syst ...
- 在电脑上用chrome浏览器调试android手机里的网页代码时,无法看到本地加载的js文件
在需要调试的js文件最顶部加上代码就可以看到了: console.log('haha'); debugger;
- Python3.x获取网页源码
Python3.x获取网页源码 1,获取网页的头部信息以确定网页的编码方式: import urllib.request res = urllib.request.urlopen('http://ww ...
- JS获取整个HTML网页代码 - Android 集美软件园 - 博客频道 - CSDN.NET
JS获取整个HTML网页代码 - Android 集美软件园 - 博客频道 - CSDN.NET JS获取整个HTML网页代码 分类: Android提高 2012-01-12 23:27 1974人 ...
- c# 获取网页源代码(支持cookie),最简单代码
/// /// 获取网页源码 public static string GetHtmls(string url, string referer = "", string cooki ...
- JSON使用——获取网页返回结果是Json的代码
public String getWebData(String strUrl){ String json = null; try { URL url = new URL(strUrl); HttpUR ...
- Android BLE与终端通信(一)——Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址
Android BLE与终端通信(一)--Android Bluetooth基础API以及简单使用获取本地蓝牙名称地址 Hello,工作需要,也必须开始向BLE方向学习了,公司的核心技术就是BLE终端 ...
- Python获取网页html代码
获取网页html代码: import requests res = requests.get('https://www.cnblogs.com/easyidea/p/10214559.html') r ...
随机推荐
- div+css遮罩层
曾被问到这个问题,不知所措,后来在网上找到了.大神文章:http://www.cnblogs.com/aspx-net/archive/2011/03/11/1981071.html 我想实现的效果没 ...
- MvvmLight for Xamarin.Forms
一.Xamarin.Forms 不使用框架时的绑定 需要注意的是BindingContent,不是DataContent <ContentPage xmlns="http://xama ...
- intellij 设置-试验过的
1.已修改的文件星号“*”标记 2.在PROJECT窗口中快速定位,编辑窗口中的文件 在编辑的所选文件按ALT+F1, 然后选择PROJECT VIEW 3.改变编辑文本字体大小 FILE -> ...
- 说说iOS中的手势及触摸
一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResp ...
- iOS的动画效果类型及实现方法
实现iOS漂亮的动画效果主要有两种方法, 一种是UIView层面的, 一种是使用CATransition进行更低层次的控制, 第一种是UIView,UIView方式可能在低层也是使用CATransit ...
- ubuntu: 环境搭建
1.修改更新源 sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak sudo gedit /etc/apt/sources.li ...
- SharePoint 2010 的企业级搜索技术文章
http://msdn.microsoft.com/zh-cn/library/ff828776(v=office.14).aspx http://msdn.microsoft.com/zh-cn/l ...
- Ubuntu修改屏幕默认亮度
sudo gedit /etc/default/grub 把GRUB_CMDLINE_LINUX="" 改成GRUB_CMDLINE_LINUX="acpi_backli ...
- java 并发编程
闭锁 一种可以延迟线程的进度直到其到达终止状态.可以用来确保某些活动直到其他活动都完成后才继续执行 例如: 确保某个计算在其需要的所有资源都被初始化了之后才继续执行. 确保某个服务在其他依赖的服务都启 ...
- PHP的反射机制(转)
介绍: PHP5添加了一项新的功能:Reflection.这个功能使得phper可以reverse-engineer class, interface,function,method and exte ...