android使用HttpURLConnection获取网站源码
来源:互联网 发布:苹果mac怎么更新不了 编辑:程序博客网 时间:2024/05/22 11:53
基本布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/send_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Request" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/response_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
主程序处理:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendRequestButton = (Button) findViewById(R.id.send_request);
responseTextView = (TextView) findViewById(R.id.response_text);
sendRequestButton.setOnClickListener(this);
}
按键监听处理:
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.send_request) {
sendRequestWithHttpURLConnection();
}
}
private void sendRequestWithHttpURLConnection() {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
try {
URL url = new URL("http://www.baidu.com");
connection = (HttpURLConnection) url.openConnection();
// 200 成功 302 从定向(页面跳转) 404资源没找到 5xx 服务器内部错误
int code = connection.getResponseCode();
while(code==301||code==302)
{
connection=(HttpURLConnection)reload(connection);
code = connection.getResponseCode();
}
if(code==200)
{
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream iStream = connection.getInputStream();
// 读取输入流
BufferedReader bReader = new BufferedReader(
new InputStreamReader(iStream));
StringBuilder response = new StringBuilder();
String line;
byte[] buffer = new byte[1024];
int len = -1;
while ((len = iStream.read(buffer)) != -1) {
line=new String(buffer,0,len);
if(line.contains("charset=gb2312")){//解析meta标签
line=new String(line.getBytes(),"gb2312");
}
response.append(line);
}
Message message = new Message();
message.what = SHOW_RESPONSE;
message.obj = response.toString();
handler.sendMessage(message);
}else{
Message message = new Message();
message.what = SHOW_ERROR;
message.obj = code+"";
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
处理网页301 302
private static URLConnection reload(URLConnection uc) throws Exception
{
HttpURLConnection huc = (HttpURLConnection) uc;
if (huc.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP || huc.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM)
// 302, 301
return reload(new URL(huc.getHeaderField("location")).openConnection());
return uc;
}
Handler消息处理:
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case SHOW_RESPONSE:
String response = (String) msg.obj;
// 在这里进行UI操作,将结果显示到界面上
responseTextView.setText("receive: "+response);
break;
case SHOW_ERROR:
Toast.makeText(getApplicationContext(), "here"+(String) msg.obj, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
};
};
最后不要忘记加上网络权限
<uses-permission android:name="android.permission.INTERNET" />
运行效果:
0 0
- android使用HttpURLConnection获取网站源码
- Android使用HttpURLConnection获取数据
- Android HttpURLConnection源码分析
- Android HttpURLConnection对象使用
- Android HttpURLConnection 基础使用
- Android HttpURLConnection使用
- Android 使用HttpURLConnection 下载
- android学习---使用HttpURLConnection
- 使用HttpURLConnection获取网页内容
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- Android中HttpURLConnection获取网址源码,子线程里更新UI
- Android 4.4以上使用HttpURLConnection底层使用OkHttp实现的源码分析
- 关于使用HttpURLConnection登录网站的问题
- Android自动化测试之Robotium之学习一
- codeVS 3115 高精度练习之减法
- 数据结构实验之栈一:进制转换(java实现)
- PHP中foreach的用法和实例
- 旧版本ubuntu更新软件的方法
- android使用HttpURLConnection获取网站源码
- Java线程和多线程(十)——TimerTask
- Web前端开发的问题汇总
- Java基础知识学习之面向对象一
- Linux基础——构建工具链
- SHELL编程基础
- Couldn't find a tree builder with the features you requested: lxml 问题解决
- 机器学习分类篇-支持向量机SVM上
- 理解jvm的ClassLoader分析-基础篇