返回
顶部

修改密码

首页 > 教程 > 网络通信 > HTTP > 正文
Android实现网络图片与html源码获取

+1

-1

收藏

+1

-1

点赞0

评论0

获取指定网址的图片: 获取指定网址的html源码,这里是搜狐的网址举例。 MainActivity package com.luku.netImage; import com.luku.Junit.testJunit; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import andro…

获取指定网址的图片:

 

获取指定网址的html源码,这里是搜狐的网址举例。

 

MainActivity

 
  1. package com.luku.netImage;  
  2.   
  3. import com.luku.Junit.testJunit;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.os.Bundle;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.EditText;  
  14. import android.widget.ImageView;  
  15. import android.widget.Toast;  
  16.   
  17. public class MainActivity extends Activity  
  18. {  
  19.     Button getImagebutton;  
  20.     EditText imagePatheditText;  
  21.     ImageView imageView;  
  22.     Button exitbButton;  
  23.     Button gethtml;  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState)  
  26.     {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.           
  30.         getImagebutton=(Button)findViewById(R.id.getImage);  
  31.         gethtml=(Button)findViewById(R.id.gethtml);  
  32.         exitbButton=(Button)findViewById(R.id.exit);  
  33.         imagePatheditText=(EditText)findViewById(R.id.ImagePath);  
  34.         imageView=(ImageView)findViewById(R.id.imageView);  
  35.           
  36.         getImagebutton.setOnClickListener(new OnClickListener()  
  37.         {             
  38.             @Override  
  39.             public void onClick(View v)  
  40.             {  
  41.                 try  
  42.                 {  
  43.                     byte[] data=testJunit.testGetImage(imagePatheditText.getText().toString());  
  44.                     Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);  
  45.                     imageView.setImageBitmap(bm);  
  46.                 } catch (Exception e)  
  47.                 {  
  48.                     Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT);  
  49.                 }  
  50.                   
  51.             }  
  52.         });  
  53.           
  54.         gethtml.setOnClickListener(new OnClickListener()  
  55.         {             
  56.             @Override  
  57.             public void onClick(View v)  
  58.             {  
  59.                 Intent intent=new Intent(MainActivity.this, SecondActivity.class);  
  60.                 MainActivity.this.startActivity(intent);  
  61.             }  
  62.         });  
  63.           
  64.         exitbButton.setOnClickListener(new OnClickListener()  
  65.         {  
  66.               
  67.             @Override  
  68.             public void onClick(View v)  
  69.             {  
  70.                 android.os.Process.killProcess(android.os.Process.myPid());  
  71.                   
  72.             }  
  73.         });  
  74.           
  75.     }  
  76. }  

 

 
  1. package com.luku.netImage;  
  2.   
  3. import com.luku.Junit.testJunit;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class SecondActivity extends Activity  
  13. {  
  14.     Button backbButton;  
  15.     TextView htmlTextView;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState)  
  18.     {  
  19.         // TODO Auto-generated method stub  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.second);  
  22.           
  23.         backbButton=(Button)findViewById(R.id.back);  
  24.         htmlTextView=(TextView)findViewById(R.id.htmlView);  
  25.         try  
  26.         {  
  27.             htmlTextView.setText(new String(testJunit.testGetHtml()));  
  28.         } catch (Exception e)  
  29.         {  
  30.             e.printStackTrace();  
  31.         }  
  32.           
  33.         backbButton.setOnClickListener(new OnClickListener()  
  34.         {  
  35.               
  36.             @Override  
  37.             public void onClick(View v)  
  38.             {  
  39.                 SecondActivity.this.finish();  
  40.                   
  41.             }  
  42.         });  
  43.     }  
  44.       
  45. }  

 

 
  1. package com.luku.Junit;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8. public class testJunit  
  9. {  
  10.     /** 
  11.      * 读取数据 
  12.      * @param inputStream 
  13.      * @return 
  14.      * @throws Exception 
  15.      */  
  16.     public static byte[] readStream(InputStream inputStream) throws Exception  
  17.     {  
  18.         byte[] buffer=new byte[1024];  
  19.         int len=-1;  
  20.         ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();  
  21.           
  22.         while((len=inputStream.read(buffer))!=-1)  
  23.         {  
  24.             byteArrayOutputStream.write(buffer,0,len);  
  25.         }  
  26.           
  27.         inputStream.close();  
  28.         byteArrayOutputStream.close();  
  29.         return byteArrayOutputStream.toByteArray();  
  30.     }  
  31.       
  32.     /** 
  33.      * 获取网上图片 
  34.      * @throws Exception 
  35.      */  
  36.     public static byte[] testGetImage(String path) throws Exception  
  37.     {  
  38.         URL url=new URL(path);  
  39.         HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
  40.         conn.setConnectTimeout(6*1000);  //设置链接超时时间6s  
  41.         conn.setRequestMethod("GET");  
  42.   
  43.         if(conn.getResponseCode()==200)  
  44.         {  
  45.             InputStream inputStream=conn.getInputStream();  
  46.             return readStream(inputStream);  
  47.         }  
  48.         return null;  
  49.     }  
  50.       
  51.     /** 
  52.      * 获取网址的html 
  53.      * @throws Exception 
  54.      */  
  55.     public static byte[] testGetHtml() throws Exception  
  56.     {  
  57.         String urlpath="http://www.sohu.com/";  
  58.         URL url=new URL(urlpath);  
  59.         HttpURLConnection conn=(HttpURLConnection)url.openConnection();  
  60.         conn.setConnectTimeout(6*1000);  //设置链接超时时间6s  
  61.   
  62.         conn.setRequestMethod("GET");  
  63.   
  64.         if(conn.getResponseCode()==200)  
  65.         {  
  66.             InputStream inputStream=conn.getInputStream();  
  67.             byte[] data=readStream(inputStream);  
  68.             return data;  
  69.         }  
  70.         return null;  
  71.     }  
  72. }  

 

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="图片路径"  
  11.     android:textSize="20sp"  
  12.     />  
  13. <EditText   
  14.     android:layout_width="match_parent"   
  15.     android:id="@+id/ImagePath"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="http://avatar.csdn.net/C/9/A/1_yf210yf.jpg"></EditText>  
  18. <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">  
  19.     <Button   
  20.             android:text="获取图片"   
  21.             android:layout_width="wrap_content"   
  22.             android:id="@+id/getImage" android:layout_height="wrap_content"></Button>  
  23.               
  24.     <Button   
  25.             android:text="获取html源码"   
  26.             android:id="@+id/gethtml"   
  27.             android:layout_width="wrap_content"   
  28.             android:layout_height="wrap_content"></Button>  
  29.               
  30. </LinearLayout>  
  31.       
  32. <ImageView   
  33.     android:id="@+id/imageView"   
  34.     android:layout_height="wrap_content"   
  35.     android:layout_width="wrap_content"></ImageView>  
  36. <Button   
  37.     android:text="退出"   
  38.     android:id="@+id/exit"   
  39.     android:layout_width="wrap_content"   
  40.     android:layout_height="wrap_content"></Button>  
  41.       
  42. </LinearLayout>  

 

second.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.       <Button   
  8.         android:text="返回"   
  9.         android:id="@+id/back"   
  10.         android:layout_width="wrap_content"   
  11.         android:layout_height="wrap_content"></Button>  
  12.     <ScrollView  
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content">  
  15.     <TextView   
  16.         android:id="@+id/htmlView"   
  17.         android:layout_width="wrap_content"   
  18.         android:layout_height="wrap_content"></TextView>  
  19.     </ScrollView>   
  20.     
  21.   
  22.       
  23. </LinearLayout>  

 

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.       package="com.luku.netImage"  
  3.       android:versionCode="1"  
  4.       android:versionName="1.0">  
  5.     <uses-sdk android:minSdkVersion="8" />  
  6.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.         <activity android:name=".SecondActivity"  
  18.                   android:label="@string/app_name">  
  19.                     
  20.              </activity>  
  21.   
  22.     </application>  
  23. </manifest>  

 


原文链接:http://blog.csdn.net/yf210yf/article/details/6870990

扫一扫在手机打开

评论
已有0条评论
0/150
提交
热门评论
相关推荐
今日要闻
换一批
热点排行