网络通信之网页源码查看 并显示屏幕上
2015-09-08 19:18
771 查看
1.主MainActivity 类代码如下:
public class MainActivity extends Activity { private EditText pathText; private TextView codeView; private Button button; private String html; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); codeView.setText(html); } }; Runnable runnable = new Runnable() { @Override public void run() { String path = pathText.getText().toString(); try { html = HtmlTool.getHtml(path); } catch (Exception e) { // TODO Auto-generated catch block Toast.makeText(MainActivity.this, "获取图片失败", 1).show(); e.printStackTrace(); } Message msg = new Message(); handler.sendMessage(msg); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pathText = (EditText) findViewById(R.id.pagepath); codeView = (TextView) findViewById(R.id.codeView); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(runnable).start(); } }); } }2.HtmlTool类代码:
public class HtmlTool { public static String getHtml(String path) throws Exception { // TODO Auto-generated method stub URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); if (connection.getResponseCode() == 200) { InputStream inputStream = connection.getInputStream(); byte data[] = read(inputStream); String html = new String(data, "UTF-8"); return html; } return null; } private static byte[] read(InputStream inputStream) throws IOException { // TODO Auto-generated method stub ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); int len = 0; byte buffer[] = new byte[1024]; while ((len = inputStream.read(buffer)) != -1) { arrayOutputStream.write(buffer, 0, len); } // arrayOutputStream.close(); inputStream.close(); return arrayOutputStream.toByteArray(); } }3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/pagepath" /> <EditText android:id="@+id/pagepath" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://blog.csdn.net/evahuangchen/article/details/48292747" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/codeView" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>4.清单文件里加权限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>5.运行结果:
相关文章推荐
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- Android第六讲——数据存储(五种)SharedPreferences、内部存储(缓存cache)、外部存储(Sdcrad)、SQLite数据库、网络存储
- HTTP学习
- 使用IOCP需要注意的一些问题~~(不断补充)
- iOS基础知识:Objective-C 之 网络请求
- TCP是如何保证包的顺序传输?
- nginx_tcp模块集成到openresty
- linux基础操作--网络配置
- 解读Android之HttpURLConnection
- [PHP自动化-进阶]003.CURL处理Https请求访问
- Android OkHttp完全解析
- tcpdump查看某个端口数据
- 【攻克Android (41)】HttpURLConnection
- http soap关系
- RESTful Web Service - http身份登录认证的实现和使用(一)
- tcpdump
- vmotion的网络要求
- HTTP详解1-工作原理
- HTTP详解2-请求、响应、缓存
- 关于 httpUrlConnection 的 setDoOutput 与 setDoInput