Android 从网上获取网页代码(未解析)

 
 
 
 
package com.yanjun;

import com.yanjun.getImage.GetHTML;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* 通过网络获取图片
* @author YanJun
*<uses-permission android:name="android.permission.INTERNET"></uses-permission>
*/
public class MainActivity extends Activity {
        

   private static final String TAG = "MainActivity";
  EditText htmlEditText = null ;
  Button button ;
        TextView htmlTextView = null;
        @Override
         public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                htmlTextView = (TextView) findViewById(R.id.textView_html);
                htmlEditText = (EditText) findViewById(R.id.editText_html);
                button = (Button) findViewById(R.id.button_get);
                button.setOnClickListener( new OnClickListener() {
        
       public void onClick(View v) {
         // TODO Auto-generated method stub
        String Url = htmlEditText.getText().toString();
         try {
          htmlTextView.setText(GetHTML.getHtml(Url));
        } catch (Exception e) {

          Toast
              .makeText(MainActivity. this, "链接延时",
                  Toast.LENGTH_LONG).show();
          Log.e(TAG, e.toString());
        }
      }
    });
        }

}
 
 
package com.yanjun.getImage;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetHTML {
   /**
    * 获取路径方法
    *    
    * @param path 路径
    * @return
    * @throws Exception
    */
   public static String getHtml(String path) throws Exception {
     // 从网络上获取html--URL对象用来封装路径
    URL url = new URL(path);
     // 打开路径链接---得到HttpURLConnection对象
    HttpURLConnection httpURLConnection = (HttpURLConnection) url
        .openConnection();
     // 通过HTTP协议请求网络HTML---设置请求方式:get/post
    httpURLConnection.setRequestMethod( "GET");
     // 设置连接超时
    httpURLConnection.setConnectTimeout(5 * 1000);
     // 从外界想手机应用内传递数据----通过输入流获取HTML数据
    InputStream inputStream = httpURLConnection.getInputStream();
     byte[] data = StreamTool.readInputStream(inputStream);
     // 从输入流中获取HTML的二进制数据----readInputStream()
    String html = new String(data);
        
     return html;

  }
}
 
 
package com.yanjun.getImage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTool {
        
   /**从输入流获取数据
    * @param inSream 输入流
    * @return
    * @throws Exception
    */
    
     public static byte[] readInputStream(InputStream inSream) throws Exception {
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
       // 定义一个缓冲区
       byte[] buffer = new byte[1024];
       int len = 0;
       // 不断的从流里读取数据---while循环---nSream.read(buffer)表示从流里读取数据到缓冲区
       // 读取到末尾时,返回值是-1;
       while ((len = inSream.read(buffer)) != -1) {
         // 将缓冲区的数据写到输出流中
        byteArrayOutputStream.write(buffer, 0, len);
      }
      inSream.close();
       return byteArrayOutputStream.toByteArray();
    }
}

你可能感兴趣的:(android,网上获取网页代码)