这些代码应该在Android2.2版本上运行,高版本可能会出错
原理图:
1、图片查看器
package com.njupt.imagelooker7;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText et_path;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_path = (EditText) findViewById(R.id.et_path);
iv = (ImageView) findViewById(R.id.iv);
}
public void look(View v) {
String path = et_path.getText().toString();
try {
URL url = new URL(path);//构建路径
HttpURLConnection conn = (HttpURLConnection) url.openConnection();//打开连接
conn.setRequestMethod("GET");//设置请求方法
conn.setConnectTimeout(5000);//设置超时时间
if(conn.getResponseCode() == 200){//根据返回码判断是否成功
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);//把流转化成图片
if(bitmap != null){
iv.setImageBitmap(bitmap);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2、网页源码查看器
package com.njupt.htmllooker;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText et_path;
private TextView html_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_path = (EditText) findViewById(R.id.et_path);
html_tv = (TextView) findViewById(R.id.html_tv);
}
public void look(View v) {
String path = et_path.getText().toString();
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode() == 200){
InputStream is = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
//把流转化成文本信息
while((len = is.read(buffer)) != -1){
bos.write(buffer,0,len);
}
String html = bos.toString();
html_tv.setText(html);
is.close();
bos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3、以下附上界面的源码
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="网页源码查看器"/>
<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="http://192.168.1.101:8080/web/home.jsp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="look"
android:text="查看网页源码"/>
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/html_tv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</ScrollView>
</LinearLayout>