Android实现网络图片与html源码获取
openkk
13年前
获取指定网址的图片:
获取指定网址的html源码,这里是搜狐的网址举例。
MainActivity
package com.luku.netImage; import com.luku.Junit.testJunit; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { Button getImagebutton; EditText imagePatheditText; ImageView imageView; Button exitbButton; Button gethtml; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); getImagebutton=(Button)findViewById(R.id.getImage); gethtml=(Button)findViewById(R.id.gethtml); exitbButton=(Button)findViewById(R.id.exit); imagePatheditText=(EditText)findViewById(R.id.ImagePath); imageView=(ImageView)findViewById(R.id.imageView); getImagebutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { byte[] data=testJunit.testGetImage(imagePatheditText.getText().toString()); Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bm); } catch (Exception e) { Toast.makeText(MainActivity.this, "获取图片失败", Toast.LENGTH_SHORT); } } }); gethtml.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this, SecondActivity.class); MainActivity.this.startActivity(intent); } }); exitbButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { android.os.Process.killProcess(android.os.Process.myPid()); } }); } }
package com.luku.netImage; import com.luku.Junit.testJunit; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class SecondActivity extends Activity { Button backbButton; TextView htmlTextView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); backbButton=(Button)findViewById(R.id.back); htmlTextView=(TextView)findViewById(R.id.htmlView); try { htmlTextView.setText(new String(testJunit.testGetHtml())); } catch (Exception e) { e.printStackTrace(); } backbButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SecondActivity.this.finish(); } }); } }
package com.luku.Junit; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class testJunit { /** * 读取数据 * @param inputStream * @return * @throws Exception */ public static byte[] readStream(InputStream inputStream) throws Exception { byte[] buffer=new byte[1024]; int len=-1; ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); while((len=inputStream.read(buffer))!=-1) { byteArrayOutputStream.write(buffer,0,len); } inputStream.close(); byteArrayOutputStream.close(); return byteArrayOutputStream.toByteArray(); } /** * 获取网上图片 * @throws Exception */ public static byte[] testGetImage(String path) throws Exception { URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(6*1000); //设置链接超时时间6s conn.setRequestMethod("GET"); if(conn.getResponseCode()==200) { InputStream inputStream=conn.getInputStream(); return readStream(inputStream); } return null; } /** * 获取网址的html * @throws Exception */ public static byte[] testGetHtml() throws Exception { String urlpath="http://www.sohu.com/"; URL url=new URL(urlpath); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(6*1000); //设置链接超时时间6s conn.setRequestMethod("GET"); if(conn.getResponseCode()==200) { InputStream inputStream=conn.getInputStream(); byte[] data=readStream(inputStream); return data; } return null; } }main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="图片路径" android:textSize="20sp" /> <EditText android:layout_width="match_parent" android:id="@+id/ImagePath" android:layout_height="wrap_content" android:text="http://avatar.csdn.net/C/9/A/1_yf210yf.jpg"></EditText> <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent"> <Button android:text="获取图片" android:layout_width="wrap_content" android:id="@+id/getImage" android:layout_height="wrap_content"></Button> <Button android:text="获取html源码" android:id="@+id/gethtml" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> <ImageView android:id="@+id/imageView" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView> <Button android:text="退出" android:id="@+id/exit" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="返回" android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/htmlView" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> </ScrollView> </LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.luku.netImage" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/app_name"> </activity> </application> </manifest>转自:http://blog.csdn.net/yf210yf/article/details/6870990