zoukankan      html  css  js  c++  java
  • Android 网络图片查看器与网页源码查看器

    在AndroidManifest.xml里面先添加访问网络的权限:

    <uses-permission android:name="android.permission.INTERNET"/>

    效果图如下

    下面是主要代码:

     1 package com.hb.neting;
     2 
     3 import java.io.InputStream;
     4 import java.net.HttpURLConnection;
     5 import java.net.URL;
     6 
     7 import android.annotation.SuppressLint;
     8 import android.app.Activity;
     9 import android.graphics.Bitmap;
    10 import android.graphics.BitmapFactory;
    11 import android.os.Bundle;
    12 import android.text.TextUtils;
    13 import android.view.View;
    14 import android.widget.EditText;
    15 import android.widget.ImageView;
    16 import android.widget.Toast;
    17 
    18 public class MainActivity extends Activity {
    19     private ImageView iv_show;
    20     private EditText et_input;
    21     private String path;
    22     private int code;
    23     private HttpURLConnection conn;
    24     @Override
    25     protected void onCreate(Bundle savedInstanceState) {
    26         super.onCreate(savedInstanceState);
    27         setContentView(R.layout.activity_main);
    28         iv_show=(ImageView) findViewById(R.id.iv_show);
    29         et_input=(EditText) findViewById(R.id.et_inpput);
    30     }
    31     @SuppressLint("ShowToast") public void chakan(View view){
    32         path = et_input.getText().toString().trim();
    33         if (TextUtils.isEmpty(path)) {
    34             Toast.makeText(MainActivity.this, "不能输入空的", 0).show();
    35             return;
    36         }
    37         new Thread(){
    38             public void run() {
    39                 try {
    40                     URL url = new URL(path);
    41                     conn = (HttpURLConnection) url.openConnection();
    42                     conn.setRequestMethod("GET");
    43                     conn.setConnectTimeout(5000);
    44                     code = conn.getResponseCode();
    45                     if(code==200){
    46                         InputStream in = conn.getInputStream();
    47                         //解析图片
    48                         final Bitmap stream = BitmapFactory.decodeStream(in);
    49                         runOnUiThread(new  Runnable() {
    50                             public void run() {
    51                                 //更新UI
    52                                 iv_show.setImageBitmap(stream);
    53                             }
    54                         });
    55                         in.close();
    56                     }
    57                 } catch (Exception e) {
    58                     e.printStackTrace();
    59                 }
    60             };
    61         }.start();
    62     }
    63 }
    View Code

    这是xml的布局:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <EditText
     8         android:id="@+id/et_inpput"
     9         android:layout_width="match_parent"
    10         android:layout_height="wrap_content"
    11         android:hint="请输入获取图片的地址:" />
    12     <Button 
    13         android:id="@+id/bt_read"
    14         android:onClick="chakan"
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:text="查看"
    18         />
    19     <ImageView
    20         android:id="@+id/iv_show"
    21         android:layout_width="match_parent"
    22         android:layout_height="match_parent"
    23         />
    24 </LinearLayout>
    View Code

    源码:http://pan.baidu.com/s/1bUgMgY

    接着看一下网页源码查看器的小案例:

      既然都涉及到网络的添加一个如上的网络权限是必不可少的了,具体操做如上所示,先看效果图:

    主要代码:

     1 package com.hb.network;
     2 
     3 import java.io.InputStream;
     4 import java.net.HttpURLConnection;
     5 import java.net.URL;
     6 
     7 import android.annotation.SuppressLint;
     8 import android.app.Activity;
     9 import android.os.Bundle;
    10 import android.os.Handler;
    11 import android.os.Message;
    12 import android.text.TextUtils;
    13 import android.view.View;
    14 import android.widget.EditText;
    15 import android.widget.TextView;
    16 import android.widget.Toast;
    17 
    18 import com.hb.utils.ReadStreamUtils;
    19 
    20 public class MainActivity extends Activity {
    21     protected static final int SUCESS = 0;
    22     protected static final int EORR = 1;
    23     private TextView tv_show; 
    24     private EditText et_input;
    25     private URL url;
    26     private String path;
    27     @SuppressLint("HandlerLeak") 
    28     private Handler handler=new Handler(){
    29         public void handleMessage(android.os.Message msg) {
    30             switch (msg.what) {
    31             case SUCESS:
    32                 String content=(String) msg.obj;
    33                 tv_show.setText(content);
    34                 break;
    35 
    36             case EORR:
    37                 Toast.makeText(MainActivity.this,"查看源码失败" , 0).show();
    38                 break;
    39             }
    40         };
    41     };
    42     @Override
    43     protected void onCreate(Bundle savedInstanceState) {
    44         super.onCreate(savedInstanceState);
    45         setContentView(R.layout.activity_main);
    46         tv_show=(TextView) findViewById(R.id.tv_show);
    47         et_input=(EditText) findViewById(R.id.et_input);
    48 
    49 
    50     }
    51     public void onclick(View view){
    52         path = et_input.getText().toString().trim();
    53         if(TextUtils.isEmpty(path)){
    54             return;
    55         }new Thread(){
    56             public void run() {
    57                 try {
    58                     url = new URL(path);
    59                     //判断从EditText获取的数据否为空
    60                     if(TextUtils.isEmpty(path)){
    61                         return;
    62                     }
    63                     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    64                     conn.setConnectTimeout(3000);
    65                     conn.setRequestMethod("GET");
    66                     int code = conn.getResponseCode();
    67                     if(code == 200){
    68                         InputStream is= conn.getInputStream();
    69                         String content = ReadStreamUtils.Read(is);
    70                         Message msg = new Message();
    71                         msg.what=SUCESS;
    72                         msg.obj=content;
    73                         handler.sendMessage(msg);
    74                     }
    75                 } catch (Exception e) {
    76                     e.printStackTrace();
    77                     Message msg = new Message();
    78                     msg.what=EORR;
    79                     handler.sendMessage(msg);
    80                 }
    81             };
    82         }.start();
    83 
    84     }
    85 }
    View Code
     1 package com.hb.utils;
     2 
     3 import java.io.ByteArrayOutputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 
     7 public class ReadStreamUtils {
     8 /**
     9  * 读取流的输入
    10  * @param is
    11  * @return
    12  * @throws IOException
    13  */
    14     public static String Read(InputStream is) throws IOException{
    15         ByteArrayOutputStream bos = new ByteArrayOutputStream();
    16         int len;
    17         byte [] buffer=new byte[1024];
    18         while((len=is.read(buffer))!=-1){
    19             bos.write(buffer,0,len);
    20         }
    21         is.close();
    22         bos.close();
    23         String temp = bos.toString();
    24         if(temp.contains("charset=utf-8")){
    25             return bos.toString("utf-8");
    26         }else if(temp.contains("charset=iso-8859-1")){
    27             return bos.toString("iso-8859-1");
    28         }
    29         return null;
    30         
    31     }
    32 
    33 }
    View Code

    及xml布局:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     tools:context="${relativePackage}.${activityClass}" >
     7 
     8     <EditText
     9         android:id="@+id/et_input"
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content"
    12         android:hint="请输入要查看源码的网址:" />
    13 
    14     <Button
    15         android:onClick="onclick"
    16         android:layout_width="match_parent"
    17         android:layout_height="wrap_content"
    18         android:text="查看"
    19         android:textSize="25sp" />
    20 
    21     <ScrollView
    22         android:layout_width="match_parent"
    23         android:layout_height="match_parent" >
    24 
    25         <TextView
    26             android:id="@+id/tv_show"
    27             android:layout_width="match_parent"
    28             android:layout_height="match_parent" />
    29     </ScrollView>
    30 
    31 </LinearLayout>
    View Code

      源码:http://pan.baidu.com/s/1bUgMgY

         http://pan.baidu.com/s/1i46RQqL 

  • 相关阅读:
    记录一次servlet页面无法跳转的血的教训
    1862-your password has expired. To log in you must change it using a client that supports expired passwords.问题解决
    Error starting static Resources java.lang.IllegalArgumentException: Document base D:eclipse*wtpwebapps* does not exist or is not a readable directory问题的解决
    MapReduce Shuffle过程的简单理解
    MapReduce实验03——排序
    zachman架构
    大型高并发与高可用缓存架构总结2
    金蝶SOA案例分析
    感悟:菜鸟弹性调度系统的架构设计
    大型高并发与高可用缓存架构总结
  • 原文地址:https://www.cnblogs.com/boket/p/6782248.html
Copyright © 2011-2022 走看看