网络html查看器
来源:互联网 发布:软件行业新技术 编辑:程序博客网 时间:2024/06/08 20:14
<uses-permission android:name="android.permission.INTERNET"/>
<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=".MainActivity" > <EditText android:id="@+id/et_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入html数据" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="查看" android:onClick="click" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView></LinearLayout>StreamTools.java
package org.gentry.htmlviewer.utils;import java.io.ByteArrayOutputStream;import java.io.InputStream;public class StreamTools {/** * 把输入流的内容转化成字符串 * * @param is * @return */public static String readInputStream(InputStream is) {try {ByteArrayOutputStream baos = new ByteArrayOutputStream();int len = 0;byte[] buffer = new byte[1024];while ((len = is.read(buffer)) != -1) {baos.write(buffer, 0, len);}is.close();byte[] result = baos.toByteArray();return new String(result);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return "获取失败";}}}MainActivity
package org.gentry.htmlviewer;import java.io.InputStream;import java.io.StreamTokenizer;import java.net.HttpURLConnection;import java.net.URL;import org.gentry.htmlviewer.utils.StreamTools;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.view.View;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {protected static final int ERROR = 1;protected static final int SHOW_TEXT = 2;private TextView tv_content;private EditText et_path;// 定义一个消息处理器private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case ERROR:Toast.makeText(MainActivity.this, "获取数据失败", Toast.LENGTH_SHORT).show();break;case SHOW_TEXT:String text = (String) msg.obj;tv_content.setText(text);break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv_content = (TextView) findViewById(R.id.tv_content);et_path = (EditText) findViewById(R.id.et_path);}public void click(View view) {final String path = et_path.getText().toString().trim();if (TextUtils.isEmpty(path)) {Toast.makeText(this, "路径不能为空", Toast.LENGTH_SHORT).show();} else {new Thread() {public void run() {try {URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5000);conn.setRequestProperty("User-Agent","Mozilla/4.0(compatible;MAIE 6.0; Windows NT 5.1; SV1; .NET4.OC; .NET4.OE; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; Shuame)");int code = conn.getResponseCode();if (code == 200) {InputStream is = conn.getInputStream();String result = StreamTools.readInputStream(is);Message msg = new Message();msg.what = SHOW_TEXT;msg.obj = result;handler.sendMessage(msg);} else {Message msg = new Message();msg.what = ERROR;handler.sendMessage(msg);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Message msg = new Message();msg.what = ERROR;handler.sendMessage(msg);}};}.start();}}}
0 0
- 网络html查看器
- 获取网络文本查看器--HTML源码
- Android -- 网络图片查看器,网络html查看器, 消息机制, 消息队列,线程间通讯
- 处理字符乱码问题 →网络html查看器
- 网络编程-图片、Html源文件查看器、get+post
- Android编程之从网络上获取图片查看器和HTML代码的查看器
- html代码查看器
- Android HTML查看器
- HTML查看器
- html文件查看器
- html查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- 网络图片查看器
- *******************网络图片查看器*************
- 网络图片查看器
- POJ2309 BST
- java script发布时要精简代码
- 博弈总结
- The Louvain method for community detection
- 微信公布文章阅读量 淘宝随即开发“新生意”
- 网络html查看器
- msyql.db误删,重新创建
- java汉字转拼音,取汉字首字母,支持繁体
- 有了malloc/free为什么还要new/delete ?
- Matlab绘图marker间距调整脚本
- 黑马程序员:动态代理
- TCP提供可靠性的方法
- Babelfish
- HDU 2571 命运