|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 青玄 于 2014-11-6 21:29 编辑
访问互联网读取网页代码:
所需要的权限:
- <uses-permission android:name="android.permission.INTERNET"/>
复制代码
读取内容的类:- package com.example.loadhtml_test.util;
- import java.io.ByteArrayOutputStream;
- import java.io.InputStream;
- public class Utils {
- public static String getInternetData(InputStream is)
- {
- try
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节数据输出流
- byte[] buffer = new byte[1024]; //字节数组
- int len = -1;
- while((len = is.read(buffer)) != -1) //将输入流里面的内容读到字节数组里面
- {
- baos.write(buffer, 0, len); //再将字节数组里面的内容写到字节数组输出流里面
- }
- String content = new String(baos.toByteArray(), "GBK"); //将这个内容转化为字符串并设置它的编码
- if(content.contains("content="text/html;charset=utf-8"")) //如果编码为utf-8的话
- {
- return new String(baos.toByteArray(),"utf-8"); //就设置utf-8编码
- }
- return content;
- }catch(Exception e)
- {
- e.printStackTrace();
- return null;
- }
- }
- }
复制代码
本程序的主类:- package com.example.loadhtml_test;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import com.example.loadhtml_test.util.Utils;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.app.Activity;
- import android.text.TextUtils;
- import android.view.Menu;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private TextView tv;
- private EditText et;
- private Handler handler;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- this.tv = (TextView) this.findViewById(R.id.ty);
- this.et = (EditText) this.findViewById(R.id.et);
- handler = new Handler(){
- @Override
- public void handleMessage(Message msg) { //主线程中接收子线程发来的消息方法
- // TODO Auto-generated method stub
- if(msg.what == 1)
- {
- tv.setText((String)msg.obj);
- }else if(msg.what == 2)
- {
- Toast.makeText(MainActivity.this, "网络链接有误", Toast.LENGTH_LONG).show();
- }
- }
-
- };
- }
-
- public void showData(View v) //点击事件的方法
- {
- final String path = et.getText().toString().trim(); //获取网址路径
- if(!TextUtils.isEmpty(path))
- {
- new Thread() //开一个子线程
- {
- public void run()
- {
- try
- {
- URL url = new URL(path); //得到网址路径,并把它装在URL里面
- //通过url获得HttpURLConnection对象
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET"); //设置请求的方法
- conn.setConnectTimeout(5000); //设置链接的时长
- conn.setRequestProperty("Accept","image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*");
- conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0)");
- int code = conn.getResponseCode(); //获得服务器发过来的返回码
-
- if(code == 200) //如果是200的话
- {
- final InputStream is = conn.getInputStream(); //从这个链接对象里面获取输入流
- final String content = Utils.getInternetData(is); //调用这个方法获取网页的代码
- if(content != null)
- {
- Message msg = Message.obtain(); //得到消息对象
- msg.what = 1; //设置消息
- msg.obj = content; //给消息一个对象传过去
- handler.sendMessage(msg); //发送消息
- }else
- {
- Message msg = Message.obtain();
- msg.what = 2;
- handler.sendMessage(msg);
- }
- }
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }.start();
- }else
- {
- Toast.makeText(this, "获取内容失败", Toast.LENGTH_LONG).show();
- }
- }
- }
复制代码 3.xml布局文件:
- <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" >
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="20"
- >
- <TextView
- android:id="@+id/ty"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- </ScrollView>
- <EditText
- android:id="@+id/et"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="showData"
- android:text="获得网页内容"
- />
- </LinearLayout>
复制代码
|
评分
-
查看全部评分
|