查看网络资源

查看网络资源

1、网络图片查看器
public class HandlerSearchImageSourceActivity extends Activity {
	private EditText imagepath;
	private ImageView imageview;
	private int code;
	private String path;
    private HttpURLConnection conn;
    //在主线程里面 创建一个消息处理器
    private Handler handler = new Handler(){
    	//接受处理消息的方法
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Bitmap bitmap = (Bitmap) msg.obj;
			imageview.setImageBitmap(bitmap);
		}
    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.handlersearchimage);
        imagepath = (EditText) findViewById(R.id.et_imagepath2);
        imageview = (ImageView) findViewById(R.id.iv_imageview2);
        imagepath.setText("http://pic.nipic.com/2007-11-09/2007119121849495_2.jpg");//在这里为了方便演示,直接将一个图片网址放上去
        
    }
    public void bt_searchimage2(View view){
    	 path = imagepath.getText().toString().trim();
    	if(TextUtils.isEmpty(path)){
    		Toast.makeText(this, "抱歉,图片的网络路径不能为空", 1).show();
    	}else{
    		new Thread(){
    			public void run(){
    				//下载网络上的图片,显示到imageview里面
    				try {
    					URL url = new URL(path);//1、创建URL对象
    					conn = (HttpURLConnection) url.openConnection();//2、通过URL对象打开http连接
    					conn.setRequestMethod("GET");//conn默认采用的是get方式获取数据
    					conn.setConnectTimeout(5000);
    					//conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,*/*");
    					code = conn.getResponseCode();
    					//200 OK   400文件不存在   503服务器内部错误
    					if(code==200){
    						//得到服务器返回的数据
    						InputStream is = conn.getInputStream();
    						final Bitmap bitmap = BitmapFactory.decodeStream(is);
    						//子线程不可以直接修改UI
    						/*runOnUiThread(new  Runnable(){
    							public void run() {
    								imageview.setImageBitmap(bitmap);
    							}
    						});*/
    						//拿着主线程的handler发消息去更新UI
    						Message msg = new Message();
    						msg.obj = bitmap;//把获取到bitmap放到消息盒子里  主线程
    						handler.sendMessage(msg);    						
    						is.close();
    						
    					}else{
    						Toast.makeText(HandlerSearchImageSourceActivity.this, "请求 失败", 1).show();
    					}
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			};
    		}.start();
    		
    	}
    }    
}
<?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"
    android:background="#ffffff" >
	<EditText
	    android:id="@+id/et_imagepath2"
	    android:inputType="text"
	    android:singleLine="true" 
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="请输入图片路径"/>
    <Button
        android:onClick="bt_searchimage2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查看图片"
        android:textColor="#000000" />
	<ImageView 
	    android:id="@+id/iv_imageview2"
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>	
</LinearLayout>

运行,点击查看图片

2、网络HTML查看器
public class SearchHtmlCodeActivity extends Activity {
	protected static final int SUCCESS = 1;
	protected static final int ERROR = 2;
	private EditText htmlpath;
	private TextView htmlcode;
	private String path;
	private HttpURLConnection conn;
	private int code;
	 private Handler handler = new Handler(){
	    	//接受处理消息的方法
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				switch (msg.what) {
					case SUCCESS:
						String result = (String) msg.obj; 
						htmlcode.setText(result);
						break;
					case ERROR:
							Toast.makeText(SearchHtmlCodeActivity.this, "访问网络失败", 0).show();
						break;
				}
			}
	    };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.searchhtmlcode);
        htmlpath = (EditText) findViewById(R.id.et_htmlpath);
        htmlpath.setText("https://www.cnblogs.com/boket/p/6782248.html");
        htmlcode = (TextView) findViewById(R.id.tv_result);
    }
    public void bt_searchhtmlcode(View view){
    	path = htmlpath.getText().toString().trim();
    	new Thread(){
    		public void run(){
    			try {
					URL url = new URL(path);
					conn = (HttpURLConnection) url.openConnection();
					conn.setRequestMethod("GET");
					conn.setConnectTimeout(5000);
					code = conn.getResponseCode();
					if(code==200){
						InputStream is = conn.getInputStream();
						String result = StreamsTools.readStream(is);
						Message msg = new Message();
						msg.what = SUCCESS;
						msg.obj = result;
						handler.sendMessage(msg);
						is.close();
					}else{}
				} catch (Exception e) {
					e.printStackTrace();
					Message msg = new Message();
					msg.what = ERROR;
					handler.sendMessage(msg);
					
				}
    		};
    	}.start();
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff" >
	<EditText
	    android:id="@+id/et_htmlpath"
	    android:inputType="text"
	    android:singleLine="true" 
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="请输入网页路径"/>
    <Button
        android:onClick="bt_searchhtmlcode"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查看源码"
        android:textColor="#000000" />
    <ScrollView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:scrollbars="none">
        <TextView 
	    android:id="@+id/tv_result"
	    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
	    android:textColor="#ff0000"/>
    </ScrollView>
</LinearLayout>

运行,点击查看源码

3、SmartImageView

activity:

public class SmartImageViewActivity extends Activity {
	private  SmartImageView  siv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.smartimageview);
        //SmartImageView  siv = new  SmartImageView(this);
        siv = (SmartImageView) findViewById(R.id.iv_smartimageview);
        siv.setImageURL("http://pic.nipic.com/2007-11-09/2007119123053767_2.jpg");
       
    }   
}

写一个SmartImageView类:

/***能够直接显示一个网络上的图片,接受一个网络上图片的地址**/
public class SmartImageView extends ImageView {
	private int code;
    private HttpURLConnection conn;
    private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			//super.handleMessage(msg);
			Bitmap bitmap = (Bitmap) msg.obj;
			setImageBitmap(bitmap);
		}
    };
	public SmartImageView(Context context) {
		super(context);
	}
	 public SmartImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public SmartImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	/***直接设置一个网络的路径给imageview去显示**/
	public void setImageURL(final String path){
			new Thread(){
				public void run(){
    				try {
    					URL url = new URL(path);//1、创建URL对象
    					conn = (HttpURLConnection) url.openConnection();//2、通过URL对象打开http连接
    					conn.setRequestMethod("GET");//conn默认采用的是get方式获取数据
    					conn.setConnectTimeout(5000);
    					//conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,*/*");
    					code = conn.getResponseCode();
    					if(code==200){
    						//得到服务器返回的数据
    						InputStream is = conn.getInputStream();
    						final Bitmap bitmap = BitmapFactory.decodeStream(is);
    						Message msg = new Message();
    						msg.obj = bitmap;//吧获取到bitmap放到消息盒子里  主线程
    						handler.sendMessage(msg);    						
    						is.close();
    					}else{}
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			};
			}.start();
	   }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff" >
	<cn.edu.hezeu.jsj.web.SmartImageView 
       android:id="@+id/iv_smartimageview"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" />
</LinearLayout>

运行:

猜你喜欢

转载自blog.csdn.net/qq_43306047/article/details/106140874