1、读取网络图片和网页源码
public class AappActivity extends Activity {
private EditText pathText;
private ImageView imgView;
private TextView webContent;
private EditText webPath;
@Override
public void onCreate(Bundle savedInstanceState) { //主方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pathText = (EditText) this.findViewById(R.id.imgPath);
imgView = (ImageView) this.findViewById(R.id.img);
webContent = (TextView) this.findViewById(R.id.webContent);
webPath = (EditText) this.findViewById(R.id.webPath);
}
public void getImage(View v) { //获取图片
// 解决 android.os.NetworkOnMainThreadExcep
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
String path = pathText.getText().toString();
try {
byte[] data = ImageService.getImage(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imgView.setImageBitmap(bitmap);
} catch (Exception e) {
Log.i("ttt", "errr");
e.printStackTrace();
Toast.makeText(getApplicationContext(), "获取失败", 3);
}
}
public void getWeb(View v) { //获取网页源码
// 解决 android.os.NetworkOnMainThreadExcep
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
String path = webPath.getText().toString();
String html;
try {
html = PageServict.getHtml(path);
webContent.setText(html);
} catch (Exception e) {
webContent.setText("异常信息");
e.printStackTrace();
}
}}
public class ImageService {
public static byte[] getImage(String path) throws Exception {
URL url = new URL(path);
// 基于HTTp协议
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
return StreamTool.read(is);
}
return null;
}
}
public class PageServict {
public static String getHtml(String path) throws Exception {
URL url = new URL(path);
// 基于HTTp协议
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
//XmlPullParser 可以解析imputstream 得到对象用listview显示
byte[] html = StreamTool.read(is);
return new String(html,"UTF-8");
}
return null;
}
}
public class StreamTool {
//读取流中数据
public static byte[] read(InputStream is) throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
is.close();
return bos.toByteArray();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="图片路径" />
<EditText
android:text="http://www.baidu.com/img/baidu_sylogo1.gif"
android:id="@+id/imgPath"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="getImage"
android:text="获取图片" />
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/img"/>
<EditText
android:text="http://www.baidu.com"
android:id="@+id/webPath"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:onClick="getWeb"
android:text="获取网页源码" />
<ScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="网页源码"
android:id="@+id/webContent" />
</ScrollView>
</LinearLayout>